mysqli_query() : execute a query on a MySQL database

Example : Object Oriented Style
<?Php
require "config.php";// Database connection file.

$query="select * from student LIMIT 0,5 ";
if ($result_set = $connection->query($query)) {
while($row = $result_set->fetch_array(MYSQLI_ASSOC)){
echo $row['id'],$row['name'],$row['class'],$row['mark']."<br>";
}
 $result_set->close();
}
?>
Syntax
$result_set=mysqli_query($connection,$query,MYSQLI_STORE_RESULT);
$result_set=$connection->($query,MYSQLI_STORE_RESULT)
$result_set is FALSE on failure , for SELECT ,SHOW or EXPLAIN we will get mysqli_result object. For other ( successful ) queries we get TRUE as return value.

$query is the query string
resultmode : MYSQLI_USE_RESULT ( default ) or MYSQLI_STORE_RESULT

MySQLI database connection file

By using fetch_array() we get an array of column data as elements. We will get NULL value if there is no more rows in resultset. ( So our while loop will end after going through all rows of returned result set )

MySQL SELECT Query

Query with Number of records using num_rows

<?Php
require "config.php";// Database connection file.

$query="select * from student ";
if ($result_set = $connection->query($query)) {
echo "Total No. of records :".$result_set->num_rows;
    $result_set->close();
}
?>
Procedural style
<?Php
require "config.php";// Database connection file.

$query="select * from student LIMIT 0,5 ";
if ($result_set = mysqli_query($connection,$query)) {
while($row = $result_set->fetch_array(MYSQLI_ASSOC)){
echo $row['id'],$row['name'],$row['class'],$row['mark']."<br>";
}
 $result_set->close();
}?>

MySQL DUMP of student table

Checking if matching record is available

$t="SELECT * FROM istudent where mark >90";

$check=$connection->query($t);
if(mysqli_num_rows($check)>0){
// record found 	
}else{
// No record found 	
}

Updating records with error message

$query="UPDATE  student SET class='Five'";
if ($connection->query($query)) {
echo "Records Updated";
}else{
echo $connection->error;
}


Podcast on MySQL database management using MySQLi connector

MYSQLI Functions mysqli_num_rows() Number of rows in result set
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer