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.
Related
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.
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.
I searched in google and I found the following command:
SELECT COUNT(*) FROM table_name
And I used the above code as follows to count all rows that there are in a table.
$number = mysqli_query($connection,"SELECT COUNT(*) FROM table_name");
$rows = mysqli_num_rows($number);
echo $rows;
But when I see the result using the echo command, I see the result as the 1 value. While number of rows in my table is 12.
The query just returns 1 row. That row contains the count of rows in it. You need to fetch the row to get the answer, just like you do to get the results from any other query.
$result = mysqli_query($connection,"SELECT COUNT(*) AS count FROM table_name");
$row = mysqli_fetch_assoc($result);
$rows = $row['count'];
echo $rows;
$rowcount = 'SELECT COUNT(1) FROM (select * from isk.edi_site where postal_code = 123456)';
$stmt= oci_parse($conn, $rowcount);
oci_execute($stmt);
$num_row = oci_fetch_assoc($stmt);
$num = count($num_row, COUNT_RECURSIVE);
echo $num;
It counts and return the number "1". When I use the same SQL query in Oracle SQL developer, it echos out 4000+ count. Where would my mistake be? The column count works well..
A COUNT() query only returns a single row so you need to grab it from the oci_fetch_assoc() call. What you're doing is counting the rows in the result set which will always be 1.
$row = oci_fetch_assoc($stmt);
echo $row['COUNT(1)'];
Or give the count an alias:
SELECT COUNT(1) mycount FROM ...
$row = oci_fetch_assoc($stmt);
echo $row['MYCOUNT'];
here is my mysql statement:
$result = mysql_query("select count(*) from usersecurity where
email='".$_SESSION['username']."'");
echo mysql_num_rows($result);
Problem is, from my understanding, its always returning the value 1, even if the username doesn't exist in the table...why is that?
Because SELECT COUNT(*) ... with return a row with a single value containing zero.
COUNT(*) will always return a row, even if the result is zero. You need to examine the result of that column:
SELECT COUNT(*) AS count FROM t1 WHERE email = "SOME NON EXISTENT EMAIL";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo $row['count']; // 0
An alternative would be to select the count as the number of rows:
SELECT TRUE FROM t1 WHERE email = "SOME NON EXISTENT EMAIL";
$result = mysql_query($query);
echo mysql_num_rows($result); // 0
A COUNT(*) query always returns a single row. If you want the $ of rows then do one of the following.
$result = mysql_query("select count(*) as ROWS from usersecurity where
email='".$_SESSION['username']."'");
$rows = mysql_fetch_assoc($result);
echo $rows['ROWS'];
or
$result = mysql_query("select * as ROWS from usersecurity where
email='".$_SESSION['username']."'");
echo mysql_num_rows($result);
You will always get one row from the result query. That value will some value greater than or equal to zero. You need to inspect the actual count returned not the number of rows returned.
Your SELECT query is returning the following:
select count(*)...
----------------------
0
Thus, mysql_num_rows($result) is appropriately returning 1 as a result. If you're looking to get the value of your count, then you need to take an approach like the following:
$result = mysql_query("select count(*) from usersecurity where
email='".$_SESSION['username']."'");
$row = mysql_fetch_array($result);
echo $row[0];