开发者

How to format a Mysql/php array for json?

开发者 https://www.devze.com 2023-03-03 10:14 出处:网络
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\"..

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

精彩评论

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