开发者

How to format a Mysql/php array for json?

Asked this before, but I've narrowed down the issue to this bit of code. Here's my code, when I run it, it just says "null"..

$getmsg = "SELECT * FROM user WHERE account_id = $id";      
$showmsg = @mysqli_query ($dbc, $getmsg);
        while ($row = mysqli_fetch_array($showmsg, MYSQLI_ASSOC)) {

$arrResults = array($row['user_username']);


} // END WHILE


// Print them out, one per line
echo js开发者_高级运维on_encode($arrResults);


First of all you have put the echo outside the loop which just echoes the last item instead of everyone and you don't check if there is a error with your query.

Instead this would be sufficient:

$getmsg = "SELECT * FROM user WHERE account_id = $id";      
$result = @mysqli_query($dbc, $getmsg) or die("Error: " . mysql_error());
$result = mysql_fetch_assoc($result);
echo json_encode($result);

It puts the result in one assoc array and then converts the whole array to json and prints it.


The problem you are likely having is in your assignment statement:

$arrResults = array($row['user_username']);

You should change it to the following:

$arrResults[] = $row['user_username'];

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜