简体   繁体   中英

php get from mysql all values by column to json_encode

I must put to json_encode all values of specifed mysql table column

$fromdate       = $_GET['fromdate'];
$getrezhiredh = safe_query("
    SELECT rezhour FROM rezhiredhours 
    WHERE rezdate = '".$fromdate."' ORDER BY rezhour
");

$rows = array();
while($r = mysql_fetch_assoc($getrezhiredh)) {
    $rows[] = $r;
}

print json_encode($rows);

With code above i have one problem. This code return result only when in table we have one row with selected data. In this case json_encode() result is

[{"rezhour":"1"}]

But when table have more than one row with selected data result don't return anything but

[]

How put to json_encode() all values selected from table?


EDIT:

I just wonder why in case when we have more rows in table with selected data, result don't give as example below

[{"rezhour": { [0] => "1",[1] => "4" }]

Instead in result we have "[]"


Thank You in advance.

try changing

mysql_fetch_assoc($getrezhiredh)

to

mysql_fetch_object($getrezhiredh)

Results from mysql_fetch_assoc are different than you think. Each row is more or less like this:

array(1)
    "rezhour" => "1"

So you access the data it like this:

while($r = mysql_fetch_assoc($getrezhiredh)) {
    $rows[] = $r["rezhour"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM