3

I am having some difficulty running some SQL code.

What I am trying to do is, find a row that contains the correct username, and then get a value from that correct row.

This is my SQL in the php:

mysql_query("SELECT * FROM users WHERE joined='$username' GET name")

As you can see, it looks for a username in users and then once found, it must GET a value from the correct row.

How do I do that?

1
  • It's just a sample, as I am saying I want to GET the value of name. Commented Aug 16, 2012 at 9:00

5 Answers 5

18

You need some additional PHP code (a call to mysql_fetch_array) to process the result resource returned by MySQL.

$result = mysql_query("SELECT name FROM users WHERE joined='$username'");

$row = mysql_fetch_array($result);

echo $row['name'];
Sign up to request clarification or add additional context in comments.

Comments

3
mysql_query("SELECT `name` FROM users WHERE joined='$username' ")

Just select the right column in your 'select clause' like above.

Edit: If you are just starting out though, you might want to follow a tutorial like this one which should take you through a nice step by step (and more importantly up to date functions) that will get you started.

4 Comments

a hint for avoiding sql injection would be nice ;)
@nyarlathotep that would be too overwhelming for OP. He/She is not yet familiar with mysql syntax.
@nyarlathotep Good point. Answer edited with a link to a nice step by step and in depth tutorial.
@aspirin: the concept can never be mentioned early enough - i would say as soon as DB access is involved it should be a topic ;) I agree, though, that solving the problem with syntax is more important. that's why I wrote "a hint [...] would be nice" ;)
2
mysql_query("SELECT name FROM users WHERE joined='$username'")

Comments

1
$q = mysql_query("SELECT * FROM users WHERE joined='$username'");

$r = mysql_fetch_array($q);

$name = $r['user_name']; // replace user_name with the column name of your table

Comments

1
mysql_query("SELECT name FROM users WHERE joined='$username' ")

Read documentation : http://dev.mysql.com/doc/refman/5.0/en/select.html

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.