Number of rows in a MySQL table? - php

Can anyone tell me what I am doing wrong here? This code doesn't echo out anything. I would like to see the number of rows containing $tag in the urlslug field.
<?php
$query = mysqli_query($mysqli, "SELECT * FROM tags WHERE urlslug='$tag'");
$num_rows = mysql_num_rows($query);
echo $num_rows;
Thanks! :)

You're using mysqli_query function, but then you have the deprecated mysql_num_rows function. Try mysqli_num_rows instead.

It is not very efficient to select everything in a table, count it, and then throw it away again. You should use the MySQL COUNT function to count the rows:
$result = mysqli_query($mysqli, "SELECT COUNT(*) as numRows FROM tags WHERE urlslug='$tag'");
$data = mysqli_fetch_array($result);
var_dump($data);

try this
mysqli_num_rows($query)

Try this
<?php
$query = mysqli_query($mysqli, "SELECT * FROM tags WHERE urlslug='$tag'");
$num_rows = mysqli_num_rows($mysqli,$query);
echo $num_rows;
?>

my ans with #Sverri M. Olsen's ans Number of rows in a MySQL table?:
When you COUNT(*) it takes in count column indexes, so it will be the best result. Mysql with MyISAM engine actually stores row count, it doensn't count all rows each time you try to count all rows. (based on primary key's column)
If the COUNT(*) is slow, you should run EXPLAIN on the query, and check if indexes are really used, and where should they be added.
and +1 for #Sverri M. Olsen

try this
$query = mysqli_query($mysqli, "SELECT * FROM tags WHERE urlslug='".$tag."'");
$num_rows = mysqli_num_rows($query);
echo $num_rows;

Related

How to execute function only if MySQL table is empty? [duplicate]

I'm trying to count the number of rows in a table and thought that this was the correct way to do that:
$result = $db->query("SELECT COUNT(*) FROM `table`;");
$count = $result->num_rows;
But counts always returns (int)1. If I use the same query in phpMyAdmin I get the right result. It sits in a table so I tried testing $count[0] as well, but that returns NULL.
What is the right way to do this?
You have to fetch that one record, it will contain the result of Count()
$result = $db->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];
Always try to do an associative fetch, that way you can easy get what you want in multiple case result
Here's an example
$result = $mysqli->query("SELECT COUNT(*) AS cityCount FROM myCity")
$row = $result->fetch_assoc();
echo $row['cityCount']." rows in table myCity.";
I find this way more readable:
$result = $mysqli->query('select count(*) as `c` from `table`');
$count = $result->fetch_object()->c;
echo "there are {$count} rows in the table";
Not that I have anything against arrays...
$result->num_rows; only returns the number of row(s) affected by a query. When you are performing a count(*) on a table it only returns one row so you can not have an other result than 1.

Total Results versus Returned Results in PHP

I am using the following php to display the number of records returned in a db search.
$sql = "SELECT COUNT(id) FROM authorsbooks WHERE author LIKE '%$searchquery%'";
$query = mysqli_query($dbc, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];
$textline1 = "Your Search Returned (<b>$rows</b>) Records";
<?php echo $textline1; ?>
This seems to work fine.
However, I cannot get the total number of records in the actual db to display.
Can anyone explain a way of getting the total number of records in the database. Btw, I have tried using $total = mysqli_num_rows($query) but it keeps returning 1 as an answer. Thanks for any help.
For that you've to fire another SQL query. Like this,
$sql = "SELECT COUNT(id) FROM authorsbooks";
$query = mysqli_query($dbc, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];
echo $rows; // will return total rows in database.
SELECT COUNT(*) FROM authorsbooks
It's true that $total = mysqli_num_rows($query) should return one row. When you do a SELECT COUNT(*) then the query returns 1 row telling you how many matches there were in the table.

mysql rows count by where clause

look at the database here ---->http://www.tiikoni.com/tis/view/?id=908d940
in the subcommentid rows, value 63 is repeating maximum time itself 7 times
i want to fetch this value in numerical form means like this
maximum rows in subcommentid is 7
i hv tried this
$query=mysqli_query("SELECT * FROM table WHERE where id='63'");
echo mysqli_num_rows($query);
but
but 63 is a variable so how can a server pre decide that 63 is repeating itself maximum nos
nd look at dix ques too cant get my desire result of max(like)
use select count(*) from.....
Or
use mysql function mysql_num_rows($query_result).
Try using this query -
$query = mysqli_query("SELECT subcommentid,COUNT(*) as count FROM table GROUP BY subcommentid ORDER BY count DESC;");
$result = mysqli_fetch_array($query);
echo $result['subcommentid'].'<br/>'; //subcomment id
echo $result['count']; // number of rows
If you want all, store in array -
$results = array();
while($row = mysqli_fetch_array($query)) {
$results[$row['subcommentid']] = $row['count'];
}
echo "<pre>";print_r($results);exit;
$query = "SELECT * FROM comments WHERE id=`7`";
$result = mysqli_query($sql, $query);
$rows = mysqli_num_rows($result);
Use mysqli_num_rows function in php to count the no.of rows. Your code should look like this
$query=mysqli_query("SELECT * FROM table WHERE where id='63'");
echo mysqli_num_rows($query); // will echo no.of rows having id 63
Hope this helps you

Pagination count returning no value

Was hoping someone could give me some help. I have been trying to learn pagination but have run into an issue. when I run this to get my total rows count:
$sql = "SELECT COUNT (*) FROM item WHERE fid='17'";
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];
$rows comes back with no value, I am under the impression that $rows should contain the total number of records from item with the fid of 17, when I run SELECT COUNT (*) FROM item WHERE fid='17' in phpmyadmin it returns 98 which is the correct count. Directly before the above code I use this code to connect to the db, which I use again later to display the records which works fine.
$con=mysqli_connect("$host","$username","$password","$dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
This statement displays records later in the script
$sql = mysqli_query($con,"SELECT * FROM item WHERE fid='17' ORDER BY id DESC $limit ");
So there is data and the info is correct.
I have been following this tutorial on the subject http://www.developphp.com/view.php?tid=1349 and it works like a charm on his example in the video, all I have changed is the database particulars to mine. Can't figure this one out, been stuck for days so now I am bothering you fine folks.
Update: Changed
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];
to
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rows=mysqli_num_rows($result);
// Free result set
mysqli_free_result($result);
}
And everything is working now. Still wish I knew what was wrong with my original code but I will be moving on. Thanks Phil Perry for all your help this morning!
Well, you could SELECT * FROM... and then call mysqli_num_rows($query). Probably slower than SELECT count(*). BTW, I presume that item is not a reserved word in SQL. You can always wrap table and field names in backticks (not quotes ' or "). You could also try SELECT count(*) AS myCount....
try this,
$sql = "SELECT COUNT(*) FROM item WHERE fid='17'";
$query = mysqli_query($sql);
$row = mysqli_fetch_array($query);
$rows = $row[0];

MySQLi count(*) always returns 1

I'm trying to count the number of rows in a table and thought that this was the correct way to do that:
$result = $db->query("SELECT COUNT(*) FROM `table`;");
$count = $result->num_rows;
But counts always returns (int)1. If I use the same query in phpMyAdmin I get the right result. It sits in a table so I tried testing $count[0] as well, but that returns NULL.
What is the right way to do this?
You have to fetch that one record, it will contain the result of Count()
$result = $db->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];
Always try to do an associative fetch, that way you can easy get what you want in multiple case result
Here's an example
$result = $mysqli->query("SELECT COUNT(*) AS cityCount FROM myCity")
$row = $result->fetch_assoc();
echo $row['cityCount']." rows in table myCity.";
I find this way more readable:
$result = $mysqli->query('select count(*) as `c` from `table`');
$count = $result->fetch_object()->c;
echo "there are {$count} rows in the table";
Not that I have anything against arrays...
$result->num_rows; only returns the number of row(s) affected by a query. When you are performing a count(*) on a table it only returns one row so you can not have an other result than 1.

Categories