mysqli_fetch_assoc() not returning anything - php

This is not the first time I have used mysqli_fetch_assoc() to get a specific column of a row. For some reason I can not figure out why it is not working this time around. Heres my code:
$sql = "SELECT * FROM database.table WHERE PushNumber > 0";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = mysqli_fetch_array($result)) {
// This will loop through each row, now use your loop here
$row = mysqli_fetch_assoc($result);
$pushNumber = $row['PushNumber'];
$followingIDs = $row['FollowingIDs'];
$usernameID = $row['Username'];
error_log("'$pushNumber", 0);
error_log("'$usernameID", 0);
}
}
There is one row in my table where PushNumber is greater than 0 and $pushNumber (type: int) and $usernameID (type: string) are printed in the error log and both are printed as: '' AND I DON'T KNOW WHY! There are no spelling errors and I know for a fact in that row PushNumber is equal to 1 and Username is equal to #yo. Any suggestions would be appreciated please help. Thank you!

You appear to assign two completely different sets of values into your $row variable.
$row = mysqli_fetch_array($result) transfers the entire array into $row, but then in your loop you use $row = mysqli_fetch_assoc($result) to transfer a single record into $row, which is most likely throwing your loop off.
Try this:
if($result->num_rows > 0)
{
while($row = mysqli_fetch_assoc($result))
{
// This will loop through each row, now capture each row's data here
$pushNumber = $row['PushNumber'];
$followingIDs = $row['FollowingIDs'];
$usernameID = $row['Username'];
error_log($pushNumber, 0);
error_log($usernameID, 0);
}
}

Related

IF-ELSE from MySQL based on single row

I try to get the value 'entryScans' from my SQL-table and send it to $output based on the IF-ELSE case. What am I doing wrong?
<?php
include ('config.php');
$TicketNo=$_POST["TicketNo"];
$hash=$_POST["hash"];
$sql = "SELECT TicketNo,hash,entryScans FROM `Tickets` WHERE `TicketNo` = '$TicketNo' AND `hash` = '$hash'" or die(mysql_error());
$result = mysql_query($sql);
$row=mysql_num_rows($result);
if($row['entryScans'] = 0){
$output="ok";
}
else if ($row['entryScans'] > 0) {
$output="maybe";
else{
$output="error";
}
print(json_encode($output));
mysql_close();
?>
You are doing ...
if($row['entryScans'] = 0){
Which has 2 problems, = is assignement, == is testing equal to. The second part is that your fetching the results in...
$result = mysql_query($sql);
$row=mysql_num_rows($result);
So $row is the number of rows, $result is the query results...
So to check the number of rows in the result set
if($row == 0){
etc.
Update:
If you want the value of the column entryScans to be used, then you need to change the call of mysql_num_rows() to mysql_fetch_assoc(), so
$row=mysql_fetch_assoc($result);
Then you can leave the rest of the code to use $row['entryScans']
You are not processing your results. You just get the total number of rows in $row variable.
You need to process your query result by looping method.
Here is the example
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
if($row['entryScans'] = 0)
{
$output="ok";
}
else if ($row['entryScans'] > 0)
{
$output="maybe";
}
else
{
$output="error";
}
}
Hope this works for you.

MySQL SELECT Command does find one entry less

here is my problem.
function getSent($id) {
include 'dbh.php';
$data = array();
$sql = "SELECT * from message WHERE senderid='$id'";
$result = $con->query($sql);
$row = $result->fetch_assoc();
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
return $data;
}
In the theory this method is picking all messages (in this case), but it always find one message less than in the db => If I drop the whole table, I will get an array with the size of 0.
How could I fix this?
Remove the first $row = $result->fetch_assoc();. You are fetching one row, then looping to fetch the rest of the data and you don't store the first fetch.
`

PHP - doesn`t show anything with mysqli_fetch_assoc in while-loop

I want to give some data from a MySQL database to a Android App via JSON.
So far i have written this php script to give me the JSON object:
$result = mysqli_query($con,"SELECT * FROM job");
$row = mysqli_fetch_assoc($result);
while($row = mysqli_fetch_assoc($result)){
$output[] = $row;
}
print(json_encode($output));
I have found out by accident that when I dont add the line "$row = mysqli_fetch_assoc($result);" before the while-loop it doesn`t return anything. But when I do add this line like in the example the JSON object doesnt contain the first element.
I believe its because of this line where $row already contains the first line.
Hopefully you can help me :)
With purpose to figure out what wrong with your query you have to do something like this:
if ($result = mysqli_query($db, $sql)) {
// Here you can see and check count of rows in your result set.
$numRows = mysqli_num_rows($result);
// ...
} else {
// Here you can see and check and understand error.
var_export(mysqli_error($db));
}
with purpose to traverse all your rows from result set you have to do:
while ($row = mysqli_fetch_assoc($result)) {
var_export($row);
}
and you must not have $row = mysqli_fetch_assoc($result) before while loop.

how to add data to array using for loop in php?

I am making a mysql query . I want to add result to an array. suppose I am selecting all user from user table. I want to get everyones name. if the row=5 i want to save every name according to the row index.
if (mysql_num_rows($query) > 0)
{
$row = mysql_fetch_array($query);
//echo ($row);
$num=mysql_num_rows($query);
echo ($num);
for ($i=1; $i<=$num;$i++){
//here I want to save all name to an array.
}
Please help.
You might be looking for something like this:
$rows = array();
// while there are more records, add them to `$rows`
while($row = mysql_fetch_assoc($result)) {
$rows []= $row;
}
Note that mysql_fetch_assoc() will just return false if there are no (more) records in the result set. So you don't need the call to mysql_num_rows()
$num = mysql_num_rows($query);
if($num > 0)
{
while($row = mysql_fetch_array($query)
{
$names[] = $row['name'];
}
}
That will create an array called $names which you can loop through later. Also, it's a good time to look into mysqli_* functions, or PDO.

Printing a MySQL query in PHP

function MattsScript()
{
$query = 'SELECT * FROM `ACCOUNTING` WHERE `ACCTSTATUSTYPE` = "start" AND `Process_status` IS NULL LIMIT 0,100';
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
while ($row = mysql_fetch_assoc($result))
{
echo $row['USERNAME'] . "<br />";
echo $row['ACCTSTATUSTYPE'];
}
}
I am trying to echo the results of a query. What I think is happening here is I am saving a query to a variable, the first 100 results (LIMIT 0,100) then using a loop to echo each row to the page.
However nothing is happening, no error and nothing written to the page.
Is there something I am missing?
if you are expecting only one result remove the while loop if not leave the while loop and remove the line $row = mysql_fetch_assoc($result); before the while loop. Also make sure you are querying your database correctly.
Example: $result = mysql_query($query) or die(mysql_error());

Categories