问题
I had a script for this that worked great in PHP 5.3. New host only supports 5.5 so I've done some modifying. So far, I can only get one row to be returned. I've been agonising for hours and I just can't see why.
Is this possibly because I'm not using array_push() after the while loop? In my original script I had it but found out with this one I got a 500 Internal Server Error with it which disappeared if I took it out.
<?php
$mysqli = mysqli_connect("localhost", "user", "password", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$row_array = array();
$query = "SELECT title,year,author,journal,keywords,abstract,url FROM entries";
if ($result = mysqli_query($mysqli, $query)) {
/* fetch object array */
while ($row = mysqli_fetch_assoc($result)) {
$row_array['title'] = $row['title'];
$row_array['year'] = $row['year'];
$row_array['author'] = $row['author'];
$row_array['journal'] = $row['journal'];
$row_array['keywords'] = $row['keywords'];
$row_array['abstract'] = $row['abstract'];
$row_array['url'] = $row['url'];
}
echo '{"data":';
echo json_encode($row_array);
echo '}';
/* free result set */
$result->close();
}
/* close connection */
$mysqli->close();
?>
What get's returned is this:
{"data":{"title":"ERGOT The Genus Claviceps","year":"1999","author":null,"journal":null,"keywords":"LSD","abstract":null,"url":null}}
Which is fine and dandy and all except there are about 9500 other rows that should be coming back with it.
回答1:
Your while loop keeps overwriting the same array elements in the $row_array array.
To do what you're trying to do: while($row = mysqli_fetch_assoc($result)) { $row_array[] = $row; }
回答2:
You need to have a multi-dimensional array and you have taken a single dimensional array.
So, every time record comes, it overwrites older one.
Hence, you are getting the last record only.
Modify your code to:
$i=0;
$row_array = array();
while ($row = mysqli_fetch_assoc($result)) {
$row_array[$i]['title'] = $row['title'];
$row_array[$i]['year'] = $row['year'];
$row_array[$i]['author'] = $row['author'];
$row_array[$i]['journal'] = $row['journal'];
$row_array[$i]['keywords'] = $row['keywords'];
$row_array[$i]['abstract'] = $row['abstract'];
$row_array[$i]['url'] = $row['url'];
++$i;
}
回答3:
$row_array= array();
while ($row = mysqli_fetch_assoc($result)) {
$tmp = array();
$tmp['title'] = $row['title'];
$tmp['year'] = $row['year'];
$tmp['author'] = $row['author'];
$tmp['journal'] = $row['journal'];
$tmp['keywords'] = $row['keywords'];
$tmp['abstract'] = $row['abstract'];
$tmp['url'] = $row['url'];
$row_array[] = $tmp;
}
echo '{"data":';
echo json_encode($row_array);
echo '}';
回答4:
Your $row_array
replaces every time and you get the last one. change your code to:
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
$row_array[$i]['title'] = $row['title'];
$row_array[$i]['year'] = $row['year'];
$row_array[$i]['author'] = $row['author'];
$row_array[$i]['journal'] = $row['journal'];
$row_array[$i]['keywords'] = $row['keywords'];
$row_array[$i]['abstract'] = $row['abstract'];
$row_array[$i]['url'] = $row['url'];
$i++;
}
echo '{"data":';
echo json_encode($row_array);
echo '}';
来源:https://stackoverflow.com/questions/34890893/while-loop-only-returns-one-row-from-9000-records