This question already has answers here:
SELECT COUNT(*) AS count - How to use this count
(5 answers)
Closed 1 year ago.
$query = "select count(*)
from relationships
where leader = 'user_id'";
$result = mysql_query($query);
how can i display the count? thanks
$count = mysql_fetch_array($result);
echo $count[0];
$query = "SELECT COUNT(*) AS total FROM table";
$result = mysql_query($query);
$values = mysql_fetch_assoc($result);
$num_rows = $values['total'];
echo $num_rows;
use $row = mysql_fetch_array($result) and access it by index 0: $row[0]
use an alias in your query ("select count(*) as cnt from ...") and $row = mysql_fetch_assoc($result) and access it by name: $row['cnt']
$abc="SELECT count(*) as c FROM output WHERE question1=4";
$result=mysqli_query($conn,$abc);
if($result)
{
while($row=mysqli_fetch_assoc($result))
{
echo $row['c'];
}
}
Its work completely in this it count the number of occurrences of 4 in the column
Related
This question already has an answer here:
<cfoutput> a MySQL SUM function
(1 answer)
Closed 12 months ago.
SELECT SUM(total_cost)
FROM purchase;
How do I make the value of SUM(total_cost) to a variable?
If SUM(total_cost) = 300, how do I assign 300 to $result for example?
I'm trying to do this in coldfusion, but php will work too.
You can completely handle it in the MySQL query:
SELECT SUM(column_name) FROM table_name;
Using PDO (mysql_query is deprecated)
$stmt = $handler->prepare('SELECT SUM(value) AS value_sum FROM codes');
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$sum = $row['value_sum'];
Or using mysqli:
$result = mysqli_query($conn, 'SELECT SUM(value) AS value_sum FROM codes');
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
For mysqli
$data = mysqli_query($conn, 'SELECT SUM(value) AS value_sum FROM codes');
$row = mysqli_fetch_assoc($data);
$result = $row['value_sum'];
Now u can get a Sum of value in $result
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I wanna ask how to display database usernames from mysql in php echo
$send = $database->query('SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1');
while ($show = mysql_fetch_array($send)) {
echo ''.$show['usernames'];
}
This fixes the php error:
$ts = $database->query('SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1');
while ($show = $ts->fetch(PDO::FETCH_ASSOC)) {
echo ''.$show['usernames'];
}
For sake of completeness you expect only 1 row of results so while-loop could be redundant:
$ts = $database->query('SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1');
$show = $ts->fetch(PDO::FETCH_ASSOC);
echo $show['usernames'];
$sql = "SELECT * FROM 'usernames` ORDER BY RAND() LIMIT 1' ";
$result = $conn->query($sql);
while($record = mysqli_fetch_array($result)) {
foreach ($record as $d){
echo $d;
}
}
Try this
$query = "SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1"
$send = mysql_query($query);
while ($show = mysql_fetch_array($send)) {
echo ''.$show['usernames'];
}
How to count mysql with using group by PHP ?
When i test this code it's echo 1
How to do for echo 3 (by count country)?
This is my table_test
_____________________
|__id___|__country__|
|__1____|____usa____|
|__2____|____usa____|
|__3____|___china___|
|__4____|____uk_____|
This is my code.
<?PHP
$result = mysql_query("SELECT COUNT(*) FROM table_test GROUP BY country");
$row = mysql_fetch_row($result);
mysql_free_result($result);
$all_country = $row[0];
echo $all_country;
?>
Try:
SELECT Country,COUNT(*) FROM table_test GROUP BY country
1) Use mysqli_* API instead of mysql_* API
2) Use mysqli_num_rows to get total row returned
$sql="SELECT COUNT(*) FROM table_test GROUP BY country";
if ($result=mysqli_query($con,$sql))
{
$rowcount=mysqli_num_rows($result);
}
echo $rowcount; // print 3
use this code for counting rows of selected query
<?php
$result = mysql_query("SELECT COUNT(*) FROM table_test GROUP BY country");
$row = mysql_fetch_row($result);
$all_country = mysql_num_rows($result);
echo $all_country;
?>
I have this query. However, it does not work properly. The echo returns always a 1, but there are 3 rows in the db
<?php
include "db_connect.inc.php";
$sql = "SELECT COUNT(id) FROM profiles";
$res = mysqli_query($con, $sql);
$num = mysqli_num_rows($res);
if ($num == 0)
echo "0";
echo $num;
mysqli_close($con);
?>
You're doing an aggregate query, which means you'll ALWAYS get one row of results - one row containing the count() value you requested. Even if that count() is 0, you'll STILL get one row of results.
If you want to check the value of the count, you have to fetch that row and check the field's value, e.g.
$sql = "SELECT COUNT(id) AS cnt FROM profiles";
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
$row = mysqli_fetch_assoc($result);
if ($row['cnt'] == 0) { die("No profiles"); }
Your query returns 1 row with value 3
To see what you do expect you need something like:
<?php
include "db_connect.inc.php";
$sql = "SELECT COUNT(id) myCount FROM profiles";
$res = mysqli_query($con, $sql);
if ($row = mysqli_fetch_array($res, MYSQLI_ASSOC) ) {
echo $row['myCount'];
} else {
echo "0";
}
mysqli_close($con);
?>
I must be exhausted because I know I have done this before but I can't remember how I did or or find that snippet. I have two tables "questions" and "answers". The primary "id" from "questions" is also "id" in "answers". I'm trying to count "answers" based on "id" from that table.
<?php
$query = "SELECT * FROM questions ORDER BY id DESC";
$result = mysql_query("$query");
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$date = $row['date'];
$question = $row['question'];
$time = date("U");
$likes = $row['likes'];
$query2 = "SELECT * FROM answers WHERE id = $id";
$num_rows = mysql_num_rows($query2);
print("<span style='font-size: .7em';><a href='qplusone.php?id=$id'>+1</a>
- Likes:$likes</span> $question - <a href='answer.php?id=$id&pp=$time'>answer it</a>
or <a href='answers.php?id=$id&pp=$time'>read $num_rows answers</a>
<span style='font-size: .7em';>($date)</span><br />");
}
?>
Well, first of all you never execute $query2. But even better than returning all the rows and counting them, simply return the number of rows counted by mysql.
$query2 = "SELECT count(*) FROM answers WHERE id = $id";
$result = mysql_query($q);
$row = mysql_fetch_array($result);
$count = $row['count(*)']; // $count holds the number of matching records.
To get the count of answers per question you can do this
select q.id,
count(*) as answers
from answer a
left join questions q on q.id = a.id
group by q.id