I have the following code:
<?php
include 'db.php';
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
$tableName = "tbl_title";
$result = mysql_query("SELECT id, fld_title FROM $tableName");
$data = array();
while ( $row = mysql_fetch_row($result) )
{
//$data[] = $row;
//$data['item'][] = $row;
//$data['item'][] = $row['id'].value;
$id = $row['id'];
$fld_title = $row['fld_title'];
$data = array( "id" => $id, "fld_title" => $fld_title);
//echo $row['id'] . " " . $row['fld_title'];
//echo '<option value="'.$row->id.'">'.$row->fld_title.'</option>';
}
echo json_encode( $data );
// echo $row[0] . " " . $row[1];
?>
I am trying to return the results in a formatted way, however, even though I get results from the query, $id and $fld_title don’t get set to a value. What am I missing?
There’s no need to reference each column.
$rowis an array already. I’ve changed to using mysql_fetch_assoc because that one gives named columns.