complete code for getting number of rows from resulset
connect with mysql database with php
suppose you have database username : "admin" and database passwoord : "admin" and database name is "newdata"
$dbconn = mysqli_connect("localhost", "admin", "admin", "newdata");
after the established connection to the database
and now run the query and generate the resultset
$sqlquery = "SELECT * from users";
$resultset = mysqli_query($dbconn, $sqlquery);
if resultset is generated ok than fetch the data or get the number of the rows by mysqli_num_rows with resultset.
if ($resultset)
{
//mysqli_num_rows return number of rows in the table.
$totalrows = mysqli_num_rows($resultset);
}
Final code will be like :
<?php
$dbconn = mysqli_connect("localhost", "admin", "admin", "newdata");
if (mysqli_connect_errno())
{
echo "Database connection failed.";
}
//prepare query
$sqlquery = "SELECT * from users";
// run the query and generate results
$resultset = mysqli_query($dbconn, $sqlquery);
if ($resultset)
{
//mysqli_num_rows return number of rows in the table.
$totalrows = mysqli_num_rows($resultset);
echo "Total records :".$totalrows;
// release the result set.
mysqli_free_result($resultset);
}
// Connection close
mysqli_close($dbconn);
?>
it is very good if you close connection by mysqli_close and also free the resultset mysqli_free_result($resultset) for better performance.