I wish to find the count of certain items from database. i used this code
$sql=" SELECT count(*) from request WHERE status = '0'";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
echo "<pre>";
print_r($row);
echo "</pre>";
}
}
i am getting this array in row
Array
(
[count(1)] => 1
)
To fetch value from this array i used
$total = $row[0];
echo $total;
but did not get any result. how can i fetch value from this array
You need to use:
$row = mysqli_fetch_row($result);
echo $row[0];
or change your query to:
$sql=" SELECT count(*) as `num` from request WHERE status = '0'";
and use:
$row = mysqli_fetch_assoc($result));
echo $row['num'];
I think you can not need if condition in your code. You can do this
$sql=" SELECT * from request WHERE status = '0'";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($result))
{
echo "<pre>";
print_r($row);
echo "</pre>";
}
Related
i have this code :
$result = mysqli_query($conn, "SELECT SUM(angsuran) FROM `laporan` WHERE id_mustahik=".$detail_campaigner->id_mustahik."");
$row = mysqli_fetch_assoc($result);
print_r($row);
when i run this code, the result is :
Array ( [SUM(angsuran)] => 30000 )
But i want it to display just "30000" number, without "Array ( [SUM(angsuran)] => ) ".
How can i do that ?
add alias in your query like
$result = mysqli_query($conn, "SELECT SUM(angsuran) as sumtotal FROM `laporan` WHERE id_mustahik=".$detail_campaigner->id_mustahik."");
$row = mysqli_fetch_assoc($result);
echo $row['sumtotal']; //outputs 30000
I have 2 ways
Use array php
$result = mysqli_query($conn, "SELECT SUM(angsuran) FROM `laporan` WHERE id_mustahik=".$detail_campaigner->id_mustahik."");
$row = mysqli_fetch_assoc($result);
echo $row["SUM(angsuran)"]; // out 30000
Modify sql
$result = mysqli_query($conn, "SELECT SUM(angsuran) as sum FROM `laporan` WHERE id_mustahik=".$detail_campaigner->id_mustahik."");
$row = mysqli_fetch_assoc($result);
echo $row["sum"]; // out 30000
Not a duplicate of Select specific value from a fetched array
I have a MySql database as:
Here's my query:
$sql = "SELECT * FROM data ORDER BY Score DESC";
I want it to be a leaderboard which people can update their scores so I can't use
$sql = "SELECT * FROM data ORDER BY Score DESC WHERE ID = 1";
I want to get Username of the second row in my query.So I wrote:
<?php
include "l_connection.php";
$sql = "SELECT * FROM data ORDER BY Score";
$result = mysqli_query($conn, $sql);
if($result->num_rows>0){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
}
echo "Result = '".$row[1]['Username']."''";
}
?>
But it returns Result = '' like there's nothing in the array.
But if I write
if($result->num_rows>0){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "Name = '".$row['Username']."''";
}
}
It will return : Parham, Mojtaba, Gomnam, Masoud,
So what am I doing wrong in the first snippet?
You can not access $row outside of while loop.
So store result in one new array, and then you can access that new array outside the while loop:
$newResult = array();
if($result->num_rows>0){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$newResult[] = $row;
}
}
echo "Result = '".$newResult[1]['Username']."''"; // thus you can access second name from array
Because you write where condition after ORDER by at
$sql = "SELECT * FROM data ORDER BY Score DESC WHERE ID = 1";
The sequence of query is
SELECT * FROM data // select first
WHERE ID = 1
ORDER BY Score DESC// Order by at last
Check http://dev.mysql.com/doc/refman/5.7/en/select.html
And for second case you need to fetch Username inside while loop and use $row['Username'] instead $row[1]['Username']
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "Result = '".$row['Username']."''";// write inside while loop
}
You can assign row value to any array and use that array.
<?php
include "l_connection.php";
$sql = "SELECT * FROM data ORDER BY Score";
$result = mysqli_query($conn, $sql);
if($result->num_rows>0){
$rows = array();
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$rows[] = $row;
}
echo "Result = '".$rows[1]['Username']."'";
}
?>
Or if you want only second highest score from column you can user limit as
$sql = "SELECT * FROM data ORDER BY Score limit 1,1";
I am doing a php to push GCM notification, before that I need to grab data from database and send to google GCM server.
Code is below:
mysql_select_db($database_gcm_gcm, $gcm_gcm);
$result = mysql_query("SELECT id, RegID FROM gcm_user ORDER BY id ASC") or die(mysql_error());
$list_arr = array(array());
for($i=0; ($row = mysql_fetch_array($result)); $i++){
$list_arr[$i]=$row;
print_r ($row . '<br>');
}
print_r($result . '<br>');
?>
It suppose to show a result like
Array
(
[0] => 1, RegID 1
[1] => 2, RegID 2
)
How ever it only shows word "Array" and Resource id #4.
Which part I am doing wrong?
Thanks
$list_arr = [];
$resultCount = mysql_num_rows($result);
if ($resultCount > 0)
{
while($row = mysql_fetch_array($result))
{
$list_arr[$i] = $row['id'].','.$row['RegID '];
}
}
You cannot print a query with a for. You have to use do while.
Try this code:
mysql_select_db($database_gcm_gcm, $gcm_gcm);
$result = mysql_query("SELECT id, RegID FROM gcm_user ORDER BY id ASC") or die(mysql_error());
$row = mysql_fetch_array($result);
do{
echo $row['id'] ." " .$row['RegID'] ."<br />";
} while ($row = mysql_fetch_array($result));
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 have the following code:
$sql_latest = "SELECT * FROM tbl_latest ORDER BY id DESC LIMIT 0,3 ";
$results_latest = $mysqli->query($sql_latest);
while($row = $results_latest->fetch_object())
{
echo $row->id;
}
How can I get the results into a array so I can do something like
echo $row[1];
echo $row[2];
echo $row[2];
I'm assuming you mean get all the rows in one array
$sql_latest = "SELECT * FROM tbl_latest ORDER BY id DESC LIMIT 0,3 ";
$results_latest = $mysqli->query($sql_latest);
$rows = array();
while($row = $results_latest->fetch_object())
{
$rows[] = $row;
}
echo $rows[0]->id;
echo $rows[1]->id;
Or, if you wanted the fields in the array:
while ($row = $results_latest->fetch_array()) {
echo $row[0]; //Prints the first column
}
you are using $results_latest->fetch_object method
how do you think what method should be used to get an array?
mysql_fetch_assoc or mysql_fetch_array
$sql_latest = "SELECT * FROM tbl_latest ORDER BY id DESC LIMIT 0,3 ";
$results_latest = $mysqli->query($sql_latest);
while($row = $results_latest->fetch_array())
{
echo $row[0];
}