I have a problem. I am using this code:
$sql = "SELECT Id, FileName FROM Templates ORDER BY DateTimeUploaded DESC";
if($result = $conn->query($sql))
{
if($result->num_rows >= 1)
{
while($row = $result->fetch_object())
{
$arrTotal["Templates"] = array($row);
}
$result->free();
}
$arrTotal["Source"] = "media/templates/";
echo json_encode($arrTotal);
}
But when I print the json, the $arrTotal["Templates"] has only one row, but it has 17 rows.
What am I doing wrong?
$arrTotal["Templates"][] = array($row);Use double squares to add an entry. This is the shortcut forarray_push().array($row), just$rowwhen you add the object to array.if($result->num_rows). Because any number not 0 will end in true.