php Print_f array error - php

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));

Related

Getting exact value from fetched array from MySql

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";

find the count of data from database

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>";
}

how to change mysql_result($res, 0, "url"); to mysqli

I don't know how to use mysqli_result like past.
is this ok? how is correct?
$res = mysqli_query($con,"SELECT `id`,`usrid`,`url` FROM site WHERE state = 'popup' AND creditsp >= 1 AND (cth < cph || cph=0) order by rand() limit 1");
$urll = mysqli_result($res, 0, "url");
$ownerid = mysqli_result($res, 0, "usrid");
$siteidd = mysqli_result($res, 0, "id");
PHP 5.4 now supports function array dereferencing:
http://php.net/manual/en/migration54.new-features.php
so, in order to get the results from sql you need to use mysqli_fetch_assoc() function.
Usage can be found here:
http://php.net/manual/en/mysqli-result.fetch-assoc.php
http://www.w3schools.com/php/func_mysqli_fetch_assoc.asp
________Small Example________
Suppose we have a DB:
Id Name Age Occupation
1. William 11 Student
2. Uname 14 Student
3. Yem 22 Teacher
$query = "SELECT * FROM persons";
$res = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($res)); \\returns an associative array to the row variable. You can use foreach loop as well to loop throgh the various data items.
{
echo $row["Id"] . "<br>";
echo $row["Name"] . "<br>";
echo $row["Age"] . "<br>";
echo $row["Occupation"] . "<br>";
}
Use fetch_assoc on the result:
$row = $res->fetch_assoc();
$urll = $row['url'];
$ownerid = $row['usrid'];
$siteidd = $row['id'];
If there are more than one row, then:
while ($row = $res->fetch_assoc()) {
// process $row
}

$row=mysql_fetch_array($result); only returns even rows

We have this code:
$rowArray;
$rowID = 1;
$query = "SELECT idCentros FROM centros";
$result = mysql_query($query);
$numrows=mysql_num_rows($result);
while($row = mysql_fetch_array($result)){
$rowArray[$rowID] = $row['idCentros'];
$rowID = $rowID +1;
}
$numrows returns 4 (the rows we have in that table)...but for an unkown reason the loop starts retrieving the 2º row next it retrieves the 4º row and then it ends the loop ($row =false). As we understand this is generic code and the table definition is like this:
column idcentros int(11) pk notnull autoincremental
column nombre mediumtext
What could be happening? Thanks in advance...
try this:
$query = "SELECT idCentros FROM centros";
$result = mysql_query($query);
$numrows=mysql_num_rows($result);
$rowArray = array();
while($row = mysql_fetch_array($result))
{
array_push($rowArray,$row);
}
I don't see why the above code shouldn't work, but ... here's how I would do it:
$rowArray = array();
$query = "SELECT idCentros FROM centros";
$result = mysql_query($query);
$numrows=mysql_num_rows($result);
while($row = mysql_fetch_row($result)){
$rowArray[] = $row[0];
}
... I believe you have $rowID set to 1 just for visualisation later, but it's pointless - you should use HTML lists or some $counter++ variable for the output.

mysqli and php fetch object

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];
}

Categories