MySQL - count total number of rows in php
<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("select count(1) FROM table");
$row = mysql_fetch_array($result);
$total = $row[0];
echo "Total rows: " . $total;
mysql_close($con);
?>
select count(*) from table of mysql in php
You need to alias the aggregate using the as
keyword in order to call it from mysql_fetch_assoc
$result=mysql_query("SELECT count(*) as total from Students");
$data=mysql_fetch_assoc($result);
echo $data['total'];
Count number of rows in Mysql using PHP7
The mysqli_
functions actually accept the link as the first argument, as it is non-optional like it is with the normal mysql_
functions.
This should work as you expect:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("host", "username", "password","db_name");
mysqli_set_charset($link, "utf8mb4");
$result = mysqli_query($link, "SELECT count(*) FROM blackandwhite");
$num_rows = mysqli_fetch_row($result)[0];
echo "$num_rows Rows\n";
Getting the count of rows in each category in a MySQL table
Depending on the DDL of your tables, your query should look like this:
SELECT c.name, COUNT(*)
FROM category c
JOIN questions q
ON q.catid = c.id
GROUP BY c.name
count number of rows in table using php
<?php
$mysqli = new mysqli("hostname", "dbusername", "dbpassword", "dbname");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("SELECT count(*) cc FROM tablename")) {
/* fetch the first row as result */
$row = $result->fetch_assoc();
printf("Result set has %d rows.\n", $row['cc']);
/* close result set */
$result->close();
}
/* close connection */
$mysqli->close();
?>
In fact it's a common question you can find the answer anywhere.
Like, http://php.net/manual/en/mysqli-result.num-rows.php
You could separate this problem in to two
- I wanna know how to connect to mysql.
- I wanna know how to write that sql instruction.
Trying to count total number of rows in a table using PHP and Mysql
$sql=$db->prepare('SELECT id FROM students');
$sql->execute();
$sql->store_results();
$total=$sql->num_rows();
$sql->close();
return$total;
Returning row data
function getStudents(){
$sql=$db->prepare('SELECT id, fname, lname FROM students');
$sql->execute();
$sql->bind_result($id,$fname,lname);
while($sql->fetch()){
$students[]=array('id'=>$id,'fname'=>$fname,'lname'=>$lname);
}
$sql->close();
return$students;
}
$students=getStudents();
//if you print $students it should look: Array([0]=>Array('id'=>1,'fname'=>'ak','lname'=>'pk')[1]=>Array('id'=>2,'fname'=>'aks','lname'=>'pks')[2]=>Array('id'=>3,'fname'=>'akss','lname'=>'pkss'))
With this array you can use a foreach.
echo"<table><th>ID</th><th>fname</th><th>lname</th>";
foreach($students as$student){
//now the array students is 'split' into student. student is now Array('id'=>1,'fname'=>'ak','lname'=>'pk'); and so on.
echo"<tr><td>".$student['id']."</td><td>".$student['fname']."</td><td>".$student['lname']."</td></tr>";
}
echo"</table>";
Same page processing.
<?php
function getStudents(){
$db=new mysqli('local','user','pass','database');//obviously put outside of function if used elsewhere
$sql=$db->prepare('SELECT id, fname, lname FROM students');
$sql->execute();
$sql->bind_result($id,$fname,$lname);
while($sql->fetch()){
$students[]=Array('id'=>$id,'fname'=>$fname,'lname'=>$lname);
}
$sql->close();
return$students;
}
$students=getStudents();
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<!--other doc head stuff here-->
</head>
<body>
<nav><ol><li><a href="">Home</a></li></ol></nav>
<!-- more body stuff blahh blahh-->
<table>
<tr>
<th>ID</th>
<th>First name</th>
<th>Last name</th>
</tr>
<?php
foreach($students as $student){
echo"<tr><td>".$student['id']."</td><td>".$student['fname']."</td><td>".$student['lname']."</td></tr>";
}
?>
</table>
</body>
</html>
Now it's all neatly within one page and you don't have to fuss about using $_GET
. Hope this helps :)
Please also note that mysql is depreciated. mysqli is now what is used.
Count how many rows you have in your db with PHP
Use COUNT(*)
You can see here: http://dev.mysql.com/doc/refman/5.7/en/counting-rows.html
SELECT COUNT(*) FROM table_name;
ex:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$query = "SELECT COUNT(*) FROM table_name";
$result = mysqli_query($mysqli,$query);
$rows = mysqli_fetch_row($result);
echo $rows[0];
Related Topics
"Connect Failed: Access Denied For User 'Root'@'Localhost' (Using Password: Yes)" from PHP Function
Send File Attachment from Form Using PHPmailer and PHP
Remove New Lines from String and Replace With One Empty Space
PHP - Insert a Variable in an Echo String
How to Generate All Permutations of a String in PHP
PHP & MySQL: MySQLi_Num_Rows() Expects Parameter 1 to Be MySQLi_Result, Boolean Given
Symfony3 Classnotfoundexception After Bundle Creation
Error With PHP Mail(): Multiple or Malformed Newlines Found in Additional_Header
Get PHP to Stop Replacing '.' Characters in $_Get or $_Post Arrays
Insert Array into MySQL Database With PHP
Upgrading My Encryption Library from Mcrypt to Openssl
How to Run a .Bat File from PHP
Glob() - Sort Array of Files by Last Modified Datetime Stamp
PHP: How to Get All Possible Combinations of 1D Array
Replace the Variable Price Range by the Chosen Variation Price in Woocommerce 3