PHP & MYSQL: Get all columns, select * from row and json encode the results -
i trying fetch array mysql , print each column value.
this have far:
get columns:
$query = "show columns user_settings"; $resultx = mysql_query($query);$temp=0;$p = array(); while ($row = mysql_fetch_array($resultx)) { $p[$temp] = $row["field"];$temp++; }
foreach column name fetch data user id = $session['id']
foreach ($p $f) { $json = array(); $newquery = "select * user_settings uid = '" . $_session['id'] ."'"; $newresult = mysql_query($newquery); while($newrow = mysql_fetch_array($newresult)) { $json[$f] = $newrow[$f]; } print json_encode($json); }
this works fine. problem array printed such:
{"column1":"data"}{"column2":"data"}{"column3":"data"}
instead i json encode print:
[{"column1":"data","column2":"data","column3":"data"}}
you can remove of because redundant.
$query = "show columns user_settings"; $resultx = mysql_query($query);$temp=0;$p = array(); while ($row = mysql_fetch_array($resultx)) { $p[$temp] = $row["field"];$temp++; }
also remove foreach loop left query , while loop query.
$json = array(); $newquery = "select * user_settings uid = '" . $_session['id'] ."'"; $newresult = mysql_query($newquery); print json_encode(mysql_fetch_assoc($newresult));
this should make json_encode() function encode multidimensional array of results.
also try removing mysql_* functions , using mysqli_* functions or pdo because mysql_* functions depreciated.
Comments
Post a Comment