rei koleci
rei koleci

Reputation: 68

how to convert sql to json in php?

With the first for loop I iterate the first array and with the second for loop the second array. If there is a match i incremennt cnt.

$emparray = array();
while($row =mysqli_fetch_assoc($sqlquery)) {
  $emparray[] = $row;
}
echo count($emparray);
echo json_encode($emparray);

return json_encode($emparray);

Upvotes: 0

Views: 327

Answers (1)

David Corral
David Corral

Reputation: 4155

I think that you want is to collect in an array the information that the SQL query returns. In that way, use this code:

$emparray = array();
$i = 0;
while($row = mysqli_fetch_assoc($sqlquery)) {
  $emparray[$i] = $row["name_colum"];
  $i++;
}

echo json_encode($emparray);
// or
return json_encode($emparray);

Note that "name_column" is the name of the column that the SQL query returns. If you have more than one column, you will have to specify which column do you want to retrieve.

Upvotes: 2

Related Questions