I thought I could find loads of examples on how to do this but nothing I find tells me how to get the values in PHP.
Say I have this code:
$query = "SELECT name, age FROM people WHERE id = 2";
$result = mysql_query($query);
How do I get the data from $result?
Pseudo code:
$name = $result['name'];
$age = $result['age'];
Did you try this?
$query = "SELECT name, age FROM people WHERE id = 2";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo $row['name'];
echo $row['age'];
You need to get a row from the result first. You can get a number-based array using mysql-fetch-row
or an associative array using mysql-fetch-assoc
.
$query = "SELECT name, age FROM people WHERE id = 2";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$name = $row['name'];
$age = $row['age'];
To note: the mysql
functions are deprecated. You should use mysqli
or PDO
instead.