select * from student where class='Four' order by id
Using the above query we will display the records by keeping them inside an html table. At the top of the script we will connect to MySQL database.<html>
<head>
<title></title>
</head>
<body >
<?Php
require "config.php"; // connection string is here
////////Query & Data Display is here/////////
$sql="select * from student where class='Four' order by id ";
echo "<table>";
foreach ($my_conn->query($sql) as $row) {
echo "<tr><td><a href=details.php?id=$row[id]>$row[name]</a></td><td>$row[class]</td></tr>";
}
echo "</table>";
/////////////////////////////////////
?>
</body>
</html>
You can see we have used the name field to display a hyper link and by clicking that the individual record can be displayed in details.php page. We have formatted the hyper link to carry the student id in query string.
<a href=details.php?id=$row[id]>$row[name]</a>
Krish Star | Four |
John Mike | Four |
Alex John | Four |
Big John | Four |
Tade Row | Four |
Gimmy | Four |
Babby John | Four |
Marry Toeey | Four |
$id=$_GET['id']; // Collecting data from query string
if(!is_numeric($id)){ // Checking data it is a number or not
echo "Data Error";
exit;
}
Now we can use this id value inside our query.
select * from student where id=:id
In above query we have collected the data for the individual record. Now we will display all the fields of the record like this.
Name | Krish Star |
Class | Four |
Mark | 60 |
Address | Krish Star_address |
Image | 4.jpg |
<html>
<head>
<title></title>
</head>
<body >
<?Php
require "config.php"; // database connection with $my_conn
//////Displaying Data/////////////
$id=$_GET['id']; // Collecting data from query string
if(!is_numeric($id)){ // Checking data it is a number or not
echo "Data Error";
exit;
}
$count=$my_conn->prepare("select * from student where id=:id ");
$count->bindParam(":id",$id,PDO::PARAM_INT,3);
if($count->execute()){
echo " Success ";
$row = $count->fetch(PDO::FETCH_OBJ);
}
echo "<table>
<tr bgcolor='#f1f1f1'><td><b>Name</b></td><td>$row->name</td></tr>
<tr><td><b>Class</b></td><td>$row->class</td></tr>
<tr bgcolor='#f1f1f1'><td><b>Mark</b></td><td>$row->mark</td></tr>
<tr><td><b>Address</b></td><td>$row->address</td></tr>
<tr bgcolor='#f1f1f1'><td><b>Image</b></td><td>$row->img</td></tr>
</table>";
////////////////////
?>
</body>
</html>
<?Php
#### FOR SQLite Database ######
// This will create the database if not exists in the same location where the script is running.
// For a different location give the path details.
//$my_conn = new PDO('sqlite:D:\\sqlite-data\\student.db');// different path
$my_conn = new PDO('sqlite:'.dirname(__FILE__).'/student.db'); // same location
$my_conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
#### end of SQLite database connection #####
###### PHP PDO with MySQL ##### For php PDO use below lines ##
/*
$host_name = "localhost"; // or different host
$database = "my_db"; // Change your database name
$username = "root"; // Your database user id
$password = "password";// Your password
//////// Do not Edit below /////////
try {
$my_conn = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
*/
####### END of PHP PDO ######
?>
<?Php require "config.php"; // MySQL connection string, $my_conn $query="SELECT * FROM student LIMIT 10"; if($stmt = $my_conn->query($query)){ echo "<table>"; while ($row = $stmt->fetch_assoc()) { echo "<tr><td><a href=details.php?id=$row[id]>$row[name]</a></td> <td>$row[class]</td></tr>"; } echo "</table>"; }else{ echo $my_conn->error; } ?>
<?Php // Collecting data from query string $id=$_GET['id']; // Checking data ,it is an integer or not if(!is_numeric($id)){ echo "Data Error"; exit; } // MySQL connection string require "config.php"; // $my_conn is here $query="SELECT * FROM student where id=?"; if($stmt = $my_conn->prepare($query)){ $stmt->bind_param('i',$id); // parameter checking $stmt->execute(); $result = $stmt->get_result(); echo "No of records : ".$result->num_rows."<br>"; $row=$result->fetch_object(); echo "<table><tr ><td><b>Name</b></td><td>$row->name</td></tr> <tr><td><b>Class</b></td><td>$row->class</td></tr> <tr ><td><b>Mark</b></td><td>$row->mark</td></tr> <tr><td><b>Address</b></td><td>$row->address</td></tr> <tr ><td><b>Image</b></td><td>$row->img</td></tr> </table><BR><BR><a href=index.php>Back to listing</a>"; }else{ echo $my_conn->error; } ?>MySQLi connection
<img src=dir_name/$row->img>
To know more about how to store file name in table and manage images along with thumbnail using MySQL database , read photo gallery script
Eagertolearn | 19-03-2013 |
Great tutorial. I was banging my head for more than 2 weeks on how to accomplish this task. Thanks for sharing. Well explained. I would like to go 1 step further. How can I get the database result opens in a new page. Thanks |
effefef | 19-07-2013 |
Simple and Good. |
Stanley | 28-09-2013 |
Hey man I was looking for this, you have safe my life, thank you very much... |
John | 14-10-2013 |
Hi how can I fix the problem with Notice: Undefined index: id. I have tested with $id = isset($_GET['id']) ? $_GET['id'] : ''; but send a Data Error; but everything is OK what am I doing wrong? |
smo1234 | 17-05-2017 |
database management using mysqli ( instead of PDO ) is added. |
B K SAHU | 05-10-2017 |
I want multiple drop down menu php code. plz help me |
Rhylex | 17-12-2018 |
how to link each data to each profile page using id |
rhodz | 04-02-2019 |
hi can I ask what are the url names of the codes like index.php? I didn't work because I don't know what will I put when I save it. Thank you |
smo1234 | 08-02-2019 |
Each record can be linked by using userid or id and details of the profile can be shown as explained above select * from student where id=:id |
smo1234 | 08-02-2019 |
File names can be anything, you must match the links to the file names. |
06-10-2021 | |
Good tutorial |