0

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'];
1
  • 3
    Have a look at mysql_fetch_array and have a look at the red box. Please consider using mysqli or PDO right from the start. Commented Aug 9, 2014 at 14:59

2 Answers 2

1

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'];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I will accept this answer when it lets me in 9min.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.