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
Related
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 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);
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;
I have a database table that has 4 records with a column _id that auto increments. When I run a query to get all records, it works but it doesn't echo out all the ids, it only points to the first rows and echos it four times. I am using PHP and MySQLi. Here is my code
Code for querying
$sql = "SELECT * FROM att_table";
$query = $conn->query($sql);
$result = $query->fetch_assoc();
Code for display
do{
echo result['_id'];
}while($query->fetch_assoc());
It outputs 1111 instead of 1234. Please what is wrong?
You're fetching each of the 4 results, so it loops the appropriate number of times; but you're only assigning the fetched result to $result once, so that's the only _id value that gets echoed
do{
echo $result['_id'];
}while($result = $query->fetch_assoc())
You also can use a foreach loop :
$sql = "SELECT * FROM att_table";
$query = $conn->query($sql);
$result = $query->fetch_assoc();
foreach($result as $data){
echo $data['_id'];
}
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];