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.
Related
Here is the code I'm using to pull the data from the table:
require_once 'connect.php';
$sql = "SELECT * FROM `db-news`";
$result = $mysqli->query($sql);
$row = mysqli_fetch_assoc($result);
while ($row = $result->fetch_assoc()) {
printf ($row['pagename'].' - To edit this page click here<br>');
}
Always the first row is ignored. I'm not calling mysqli_fetch_assoc twice as with some other examples on SO. I've tried changing echo to printf in the while loop and still the first row is ignored in the DB.
I'm at a loss as to what I should try next?
mysqli not ignoring it but actually you are fetching first row before while loop
$row = mysqli_fetch_assoc($result); //remove this line
while ($row = $result->fetch_assoc()) {
....
}
The problem is the first row of the following excerpt:
$row = mysqli_fetch_assoc($result);
while ($row = $result->fetch_assoc()) {
The mysqli_fetch_assoc already gets the first row (and thus in the while loop you are already 1 step further).
You should either put that line instead of the $reslut->fetch_assoc part into the while statement or delete it. That should solve the problem.
mysqli_fetch_assoc() is the same as $result->fetch_assoc() and whenever you call this function it will advance an internal pointer to the next row. You are calling this function once just before the loop which means that you are reading the first row and ignoring the result. Remove that line.
If you need to fetch the first line and then still loop through the whole result use foreach instead of while. For example:
require_once 'connect.php';
$sql = "SELECT * FROM `db-news`";
$result = $mysqli->query($sql);
foreach($result as $row) {
printf($row['pagename'].' - To edit this page click here<br>');
}
Use do while instead of while as it will not skip the first step.
I have a mysql database of countries, 250 in total, which in want to give over to an Android app. I know i have to use php in between to parse the result into JSON. This is it:
<?php
require_once('connection.php');
$response = array();
$resultarray = array();
$result = mysqli_query($con, "SELECT * FROM countries");
if (!empty($result)) {
// check for empty result
while ($row=mysqli_fetch_assoc($result)) {
print $row;
$resultarray = mysqli_fetch_array($result);
$Laender = array();
$Countries[de] = $resultarray["de"];
$response["Countries"] = array();
array_push($response["Countries"], $Countries);
echo json_encode($response);
}
}
?>
I ran the script in my browser and it displays correctly, except that half the countries are missing. There are only 125 countries displayed. Where have they vanished to?
Both functions mysqli_fetch_assoc and mysqli_fetch_array do the same - they fetch next record.
So, in your while you fetch first record with mysqli_fetch_assoc and then immediately fetch second record with mysqli_fetch_array. So, the first record is lost. And this happens on every iteration, so half of your records are lost.
Get rid of mysqli_fetch_array call:
$response["Countries"] = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push($response["Countries"], $row["de"]);
}
echo json_encode($response);
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.
`
I need to select multiple comments (if there are any) based on the photo_id. As I understand it you can use the WHERE clause but I'm not exactly sure how to select multiple ones and store them in some kind of array?
e.g.
$result = mysqli_query($conn,"SELECT * FROM comments WHERE photo_id='$photo1id'");
$row = $result->fetch_assoc(); // but there's more than 1 row
If for example $photo1id == 21, how do I get all the comments (2 in this case)? Some kind of while loop?
At the end of the PHP file I have this:
echo json_encode(array('photo1id'=>$photo1id));
I need to store each row in that array somehow because I need to retrieve the data in another PHP file using $.getJSON. Or perhaps there is a better solution to this.
Loop through it and generate an array -
while($row = $result->fetch_assoc()) {
$comments[] = $row;
}
After that you can send the array as json.
echo json_encode($comments);
Is there is more rows, you need to use a loop.
while ($row = $result->fetch_assoc()) {
// your code here
}
Try the code below:
//Run query
$result = mysqli_query($conn,"SELECT * FROM comments WHERE photo_id='$photo1id'");
//While there is a result, fetch it
while($row = $result->fetch_assoc()) {
//Do what you need to do with the comment
}
If you don't want to print the code straight away you can just create an array:
$x=0;
while($row = $result->fetch_assoc()) {
$comment[$x]=$row['comment'];
$x++;
}
Is it possible to extract a value from mysqli_result without fetching a row i.e. without modifying the result set as I need it in full later in my code?
You can also point the iterator back to the beginning. ie..
$result = $mysqli->query($query);
while($row = $result->fetch_assoc())
{
//do stuff with the result
}
//back to the start
mysqli_data_seek($result,0);
now the pointer is returned to the beginning. mysqli_data_seek
You can get all and store in an array…
$results = [];
$sql = mysqli_query($con,"SELECT * FROM WHATEVER");
while ($row = mysqli_fetch_array($sql)) {
array_push($results, $row);
}
//Now $results is fully populated