<?Php
require "config.php";// Database connection details, $connection object
if($stmt = $connection->query("SELECT id, name ,class, mark FROM student")){
echo "No of records : ".$stmt->num_rows."<br>";
while ($row = $stmt->fetch_assoc()) {
echo $row['id'],$row['name'],$row['class'].$row['mark']."<br>";
}
}else{
echo $connection->error;
}
?>
We will get error message if the query fails to execute.
MySQLI database connection file
if($stmt = $connection->query("SELECT id, name ,class, mark FROM student")){
echo "No of records : ".$stmt->num_rows."<br>";
echo "<table class='table table-striped'>
<tr class='info'> <th> ID</th><th>Name</th><th>Class</th><th>Mark</th></tr>";
while ($row = $stmt->fetch_assoc()) {
echo "<tr><td>$row[id]</td><td>$row[name]</td><td>$row[class]</td><td>$row[mark] </td></tr>";
}
echo "</table>";
}else{
echo $connection->error;
}
<?Php
require "config.php";// Database connection having $connection
//////////////////////////////
$id=3;
if($stmt = $connection->prepare("SELECT id, name ,class, mark FROM student WHERE id=?")){
$stmt->bind_param('i',$id);
$stmt->execute();
$result = $stmt->get_result();
echo "No of records : ".$result->num_rows."<br>";
$row=$result->fetch_object();
echo $row->name;
}else{
echo $connection->error;
}
?>
Multiple records with string parameter
<?Php
require "config.php";// Database connection having $connection
//////////////////////////////
$class='Three';
if($stmt = $connection->prepare("SELECT id, name ,class, mark FROM student WHERE class=?")){
$stmt->bind_param('s',$class);
$stmt->execute();
$result = $stmt->get_result();
echo "No of records : ".$result->num_rows."<br>";
while ($row = $result->fetch_assoc()) {
echo $row['id'],$row['name'],$row['class'].$row['mark']."<br>";
}
}else{
echo $connection->error;
}
?>
Procedural style ( with two binding parameters )
<?Php
require "config.php";// Database connection having $connection
//////////////////////////////
$class='Three';
$mark=60;
if ($stmt = mysqli_prepare($connection, "SELECT id, name ,class, mark
FROM student WHERE class=? AND mark >?")) {
mysqli_stmt_bind_param($stmt, "si", $class,$mark);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
echo " No of records :".mysqli_stmt_num_rows($stmt)."<br>";
mysqli_stmt_bind_result($stmt, $id,$name,$class,$mark);
while (mysqli_stmt_fetch($stmt)) {
echo "$id, $name,$class,$mark <br>";
}
mysqli_stmt_close($stmt);
}else{
echo mysqli_error($connection);
}
?>
require "config.php";// Database connection having $connection
try {
$class = 'Three'; // Example data
$mark = 50; // Wildcard for name search
// Prepare the SQL query
$query = "SELECT * FROM student WHERE class = ? AND mark >= ?";
// Prepare the statement
$step = $connection->prepare($query);
// Bind parameters to the placeholders (s = string, i=integer )
$step->bind_param("si", $class, $mark);
// Execute the query
$step->execute();
// Get the result
$result = $step->get_result();
// Fetch all results as an associative array
while ($row = $result->fetch_assoc()) {
echo "ID: " . $row['id'] . " - Name: " . $row['name'] . " - Class: " . $row['class'] . "<br>";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
ID: 2 - Name: Max Ruin - Class: Three
ID: 3 - Name: Arnold - Class: Three
ID: 27 - Name: Big Nose - Class: Three
PHP code generator using MySQLi functions to display records of a table

Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.