Syntax error on echo that displays assoc array
Posted: Mon Mar 28, 2016 8:46 am
PHP beginner. Statement comes from code that is initializing a MySQL db. I use the echo's to help me with testing.
Had things working before I needed to convert to MySQLi. Couldn't find a direct replacement for mysql_result so moved to mysqli_fetch_assoc.
The old technique created a variable for each field which a coded echo "<br> $field1 $field2 $field3".
The new technique creates an assoc array. The best I've been able to figure is an echo like this:
$row=mysqli_fetch_assoc($result) or die(mysql_error());
echo "{$row['Locationnum'] {$row['Neighborhood'] $row['Udate']} </b><br> {$row['Address1'] $row['Address2'] $row['City'] $row['State'] $row['Zip'] $row['Owner'] $row['Status'] $row['Gas'] $row['Water'] $row['Electricity'] $row['Fire'] $row['Residents'] $row['Phone1'] $row['Description1'] $row['Phone2'] $row['Description2'] $row['Phone3'] $row['Description3'] $row['Resourceskill'] $row['Resources']} <br> {$row['Skills'] $row['Needs']} <br><br>";
The syntax error is with the braces needed to identify the variables. To put them in for all the variables is tedious, although the syntax error gets resolved. Is there a better way to display all the fields in a record via echo?
Had things working before I needed to convert to MySQLi. Couldn't find a direct replacement for mysql_result so moved to mysqli_fetch_assoc.
The old technique created a variable for each field which a coded echo "<br> $field1 $field2 $field3".
The new technique creates an assoc array. The best I've been able to figure is an echo like this:
$row=mysqli_fetch_assoc($result) or die(mysql_error());
echo "{$row['Locationnum'] {$row['Neighborhood'] $row['Udate']} </b><br> {$row['Address1'] $row['Address2'] $row['City'] $row['State'] $row['Zip'] $row['Owner'] $row['Status'] $row['Gas'] $row['Water'] $row['Electricity'] $row['Fire'] $row['Residents'] $row['Phone1'] $row['Description1'] $row['Phone2'] $row['Description2'] $row['Phone3'] $row['Description3'] $row['Resourceskill'] $row['Resources']} <br> {$row['Skills'] $row['Needs']} <br><br>";
The syntax error is with the braces needed to identify the variables. To put them in for all the variables is tedious, although the syntax error gets resolved. Is there a better way to display all the fields in a record via echo?