Count function not counting all rows in db table - php

I am trying to count the number of rows in a table, using the count function. I currently have 5 rows in the table in question. However the count function is only counting 1 row. Why is this? Any suggestions.
$count_pcode = mysqli_query($dbc, "SELECT COUNT(*)FROM Delivery_Pcode");
$count_row =mysqli_num_rows($count_pcode);
printf("%d results.\n",$count_row);
mysqli_free_result($count_pcode );
mysqli_close($dbc);

Just change the first line to:
$count_pcode = mysqli_query($dbc, "SELECT * FROM Delivery_Pcode");
The next line of code will then tell you how many rows your query return
$count_row =mysqli_num_rows($count_pcode);

This code should count the number of rows, and do so efficiently.
$result = mysql_query($dbc, "SELECT COUNT(*)FROM Delivery_Pcode");
// Verify it worked
if (!$result) echo mysql_error();
$row = mysql_fetch_row($result);
// Should show you an integer result.
print_r($row);
mysqli_free_result($result);
mysqli_close($dbc);

Try the following:
$count_pcode = mysqli_query($dbc, "SELECT * FROM Delivery_Pcode");
$count_row = mysqli_num_rows($count_pcode);

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.

How can I count all rows that there are in a 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;

PHP Count Rows In Oracle Database

I am trying to count the total number of rows in a single table. Just need to know how many rows total and nothing else. I am using Oracle database.
I have tried
$sql = "SELECT COUNT(*) AS homes FROM homes";
But I am unsure on how to display the result.
$sql = "SELECT COUNT(*) AS count FROM homes";
To count the rows from
$sql = "SELECT * FROM homes"; // fetch the rows
$sql1=mysql_query($sql);
$count=mysql_num_rows($sql1) // count number of rows in table
echo $count; //$count has the value of rows in table..
Use this code:
// Code goes here
<?php
$sql = "SELECT COUNT(*) AS homes FROM homes";
$result = mysqli_query($sql);
$row = mysqli_fetch_array($result);
print($row['homes']); // prints count
?>
<?php
$link = mysqli_connect("host", "username", "password");
mysqli_select_db("database", $link);
$result = mysqli_query("SELECT * FROM homes", $link);
$num_rows = mysqli_num_rows($result);
echo "$num_rows Rows\n";
?>

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

Categories