My code fetches several fields from a row in MySQL, makes a MYSQL_ASSOC array via mysql_fetch_array(), and formats it to JSON to return to Javascript in the browser. This part works. I want to additionally insert a PHP generated variable and its value to describe the time. A field exists for this variable and the timestamp code works in another script to insert the time of the original first post. In this script, I want the value to be the time a second PHP update script runs, not the original time value in the database. Despite a lot of trying, I can't get this last part to work. Here's relevant code:
$timeStamp = date("Ymd");
$userIP = $_SERVER['REMOTE_ADDR'];
$postingID = "$timeStamp-$userIP-u";
$latestUpdateID = str_replace(".", "", "$postingID")
//some pre-processing to make $fields_sql, an array to use in the SELECT
$fetch = mysql_query("SELECT $fields_sql FROM residence WHERE propertyID = '$propertyID'");
if( mysql_num_rows( $fetch ) ){
while ( $row = mysql_fetch_array( $fetch, MYSQL_ASSOC ) ){
for( $i = 0; $i < count( $propertyFields ); $i++ ) {
$row_array[ $propertyFields[$i] ] = mysql_real_escape_string( $row[ $propertyFields[$i] ] );
}
array_push( $return_arr,$row_array );
//get [{"city":"Rochester","streetAddress":"100 Main", etc.}]
}
$propertyFields[ 'latestUpdateID' ] = $latestUpdateID; //get "latestUpdateID":""
array_push( $return_arr,$propertyFields[ 'latestUpdateID' ] ); //pushed in but no value
//want [{"city":"Rochester","streetAddress":"100 Main", "latestUpdateID":"20131107", etc.}]
} else { die('<li class=error>Ooops</li> }
echo json_encode( $return_arr );
Two comments: 1. As you can see the query returns only 1 row. The while loop is probably not needed, but I don't know how to do it any other way. Suggestions, that also fix the problem, would be doubly appreciated. 2. Yes, I know the mysql_ family of commands is deprecated. I have several of these and my next project is to convert them all to PDO, but at the moment, I understand PDO a lot less than this stuff.
mysql_real_escape_string()in the for loop? That definitely isn't necessary, and potentially harmful to your data form if you're sending this down as JSON.json_encode()will handle the all the correct encoding.$propertyFieldshave numeric or string keys? I see you treat it both ways.