Forum Moderators: coopster

Message Too Old, No Replies

Just learning - need help.

Obtaining id from mysql, when I know the email add.

         

digisales

4:32 pm on Apr 22, 2009 (gmt 0)

10+ Year Member



The statement below returns the email address:

<?php $db = mysql_connect("localhost", "uname","pwd"); mysql_select_db("dbname",$db); $result = mysql_query("SELECT * FROM tabname WHERE id=1",$db); $myrow = mysql_fetch_array($result); echo "".$myrow["user_email"];?>

However, when I reverse it and use the email to request the id, like this:

<?php $db = mysql_connect("localhost", "uname","pwd"); mysql_select_db("dbname",$db); $result = mysql_query("SELECT * FROM tabname WHERE user_email=abc@xyz.com",$db); $myrow = mysql_fetch_array($result); echo "".$myrow["id"];?>

it does not work, and I get the following error message,
"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/demo/index.php on line 39"

Where am I going wrong?

Any help will be much appreciated. Thank you.

Pico_Train

5:40 pm on Apr 22, 2009 (gmt 0)

10+ Year Member



id is an integer or number

email is a string.

You will probably need to put the email in quotes

Try

"'.abc@xyz.com.'"
or
"abc@xyz.com"
or
'abc@xyz.com'

so your statement would be using the 3rd example above:

<?php $db = mysql_connect("localhost", "uname","pwd"); mysql_select_db("dbname",$db); $result = mysql_query("SELECT * FROM tabname WHERE user_email='abc@xyz.com'",$db); $myrow = mysql_fetch_array($result); echo "".$myrow["id"];?>

or

<?php $db = mysql_connect("localhost", "uname","pwd"); mysql_select_db("dbname",$db); $result = mysql_query('SELECT * FROM tabname WHERE user_email="abc@xyz.com"',$db); $myrow = mysql_fetch_array($result); echo "".$myrow["id"];?>

digisales

6:14 pm on Apr 22, 2009 (gmt 0)

10+ Year Member



Thank you, that did the trick.
As a follow on question . .
If I pass the user_email along in the url (like. . page.php?user_email=abc@xyz.com)

how can I get it to incorporate into your corrected statement? I have tried it like the following and it does not work:

<?php $db = mysql_connect("localhost", "zow_walt","283337"); mysql_select_db("zow_phplogin",$db); $result = mysql_query("SELECT * FROM usersd WHERE user_email='<?php echo $user_email; ?>'",$db); $myrow = mysql_fetch_array($result); echo "".$myrow["id"];?>

I also tried it in double quotes (like "<?php echo $user_email; ?>" ), but this also failed.

Pico_Train

9:12 am on Apr 23, 2009 (gmt 0)

10+ Year Member



if you are passing it on in the url it becomes $_GET['user_email']

HelenDev

9:57 am on Apr 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know you're just learning at the moment, but you might want to be careful about putting $_GET['user_email'] directly into the query. Google 'SQL injection attacks' for more info on the potential security risks of putting user entered data into a query without validating/filtering for any nasties.

[edited by: HelenDev at 9:57 am (utc) on April 23, 2009]

Pico_Train

5:05 pm on Apr 23, 2009 (gmt 0)

10+ Year Member



Indeed.

The function is mysql_real_escape_string()

Make sure you visit php.net to further your function knowledge too. Lots of good stuff there.