开发者

How to reverse an array in PHP that is fetched from MYSQL

I am trying to pull out a row of data from MySQL and put it in an array and then reverse it before displaying the results and then if confirmed by the user it will post the reversed results back into mysql

I am using t开发者_如何学Pythonhis code :

for($i=0;$i<6;$i++) {
// Make a MySQL Connection
$query = "SELECT * FROM databasedemo WHERE id='$i'"; 
$result = mysql_query($query)or die(mysql_error());
$row = mysql_fetch_array($result);
array_reverse($row,true);
echo $i."--"."A".$row['A']. " - ". "B".$row['B']. " - ". "C".$row['C']. " - ". "D".$row['D']. " - ". "E".$row['E'];
echo "<br>";
}

I am getting this error

Warning: array_reverse() [function.array-reverse]: The argument should be an array in /home/nlp4mark/public_html/Databasedemo/main.php on line 37

Any help would be really appreciated. Thanks.


You should only be executing this query once and then iterating through the results and displaying them:

$query = "SELECT * FROM databasedemo ORDER BY id DESC";


$query = "SELECT * FROM databasedemo WHERE id='$i'"; 

That query will return a one record at the time. You are sending it 6 times with the for loop, that's why you get all the records, but you send 6 times a slighty different query.

try sending one query that return all the 6 records and then reverse it. It would be something like this

$sql = "SELECT * FROM databasedemo WHERE 1=1";
for($i=0;i<6;$i++){
    $sql .= " AND id=$i";
}


do like this

select * from  (SELECT * from messages INNER JOIN logindata ON messages.author = logindata.id ORDER BY messages.mid DESC LIMIT 0,10) as t ORDER BY t.mid asc
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜