0

For the code below i am trying to get the gender value in gen then store it in $gender but i am getting wrong output. I am getting Resource id #8 as my output instead of male or female. What shall i do to get the gender value and store it in $gender with use of minimal coding.

$gender=mysql_query("SELECT `gender` as gen FROM `register` WHERE `reg_id` = 'reg252'");
echo $gender;

One way i can do is by calling something like the way below -

$gender=mysql_query("SELECT `gender` FROM `register` WHERE `reg_id` = 'reg252'");
while($erow = mysql_fetch_array($gender))
{
    echo $erow['gender'];
}
4
  • Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Aug 22, 2012 at 8:43
  • Well, yes, mysql_query returns a result set resource, and you have figured out yourself how to get results from that result set. That's the way it works. Commented Aug 22, 2012 at 8:45
  • btw it should be echo $erow['gen']; Commented Aug 22, 2012 at 8:46
  • it copy paste problem i corrected the code below as gen is removed Commented Aug 22, 2012 at 10:02

3 Answers 3

1
list($gender) = mysql_fetch_row(mysql_query("SELECT `gender` as gen FROM `register` WHERE `reg_id` = 'reg252'"));
Sign up to request clarification or add additional context in comments.

4 Comments

I'd advice against such one-liners, you should properly error handle the result of any and all mysql_query calls, not blindly feed them into the next function.
I'd advice to use PDO instead of mysql_ functions but obviously this guy is still learning
In any case, PDO or mysql_, if you want to use one-liners, you should make helper functions with proper returns and exception handling.
OK so if you put next line if(is_null($gender)) you can handle your error.
0

You are returning the column as gen, so you have to check gen echo $erow['gen'];

In general check the comment from PeeHaa

3 Comments

Sorry to see other answers mentioning the same. When I submitted my answer, no answers were here. Probably we submitted them in the same second.
coorected the second one as gen was mistakenly there
did you try to run this Query direct on the DB, and ensure that it returns a value? "SELECT gender FROM register WHERE reg_id = 'reg252'"
0

You have in your SQL

SELECT `gender` as gen ...

While you output echo $erow['gender'];

So either output echo $erow['gen'];, either SELECT gender FROM ...

2 Comments

+1'd this as it is part of the solution (which was re-done by Mina)
probably it's downvoted because you made another logical error. So either output echo $erow['gender'];, either SELECT gender FROM...It should be So either output echo $erow['gen'];, either SELECT gender FROM

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.