Novice question about using a variable passed in an url.

  • digisales
  • Newbie
  • Newbie
  • digisales
  • Posts: 9

Post 3+ Months Ago

The following statement references the user_email, in order to return the record id.

However, I want to pass the user_email along in an url (like. . page.php?user_email=abc@xyz.com), rather than than hard code it into the query. I have tried it like the following configuration and it does not work:

Code: [ Select ]
<?php $db = mysql_connect("localhost", "uname","pswd"); mysql_select_db("dbase",$db); $result = mysql_query("SELECT * FROM tabname WHERE user_email='<?php echo $user_email; ?>'",$db); $myrow = mysql_fetch_array($result); echo "".$myrow["id"];?>


I suspect that placing <?php echo $user_email; ?> within another query if the problem, and there is likely a completely different approach that is required. Any help will be much appreciated.
Thank you.
Moderator Remark: Added [code] tags
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8413
  • Loc: USA

Post 3+ Months Ago

The following code should work.
PHP Code: [ Select ]
<?php
$db = mysql_connect("localhost", "uname","pswd");
mysql_select_db("dbase", $db);
$user_email = mysql_real_escape_string($_GET['user_email']);
$result = mysql_query("SELECT * FROM tabname WHERE user_email='$user_email'",$db);
$myrow = mysql_fetch_array($result);
echo $myrow["id"];
?>
  1. <?php
  2. $db = mysql_connect("localhost", "uname","pswd");
  3. mysql_select_db("dbase", $db);
  4. $user_email = mysql_real_escape_string($_GET['user_email']);
  5. $result = mysql_query("SELECT * FROM tabname WHERE user_email='$user_email'",$db);
  6. $myrow = mysql_fetch_array($result);
  7. echo $myrow["id"];
  8. ?>
  • digisales
  • Newbie
  • Newbie
  • digisales
  • Posts: 9

Post 3+ Months Ago

Thanks for your response. I tried it, but got the following:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in public_html/demo/TESTCODE2.PHP on line 9

Are you allowing for the fact that I am trying to pull the user_email out of the url, as in "page.php?user_email=abc@xyz.com"?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8413
  • Loc: USA

Post 3+ Months Ago

Yes, that is where $_GET['user_email'] is coming from... try the following and tell me what kind of error you are seeing.
PHP Code: [ Select ]
<?php
$db = mysql_connect("localhost", "uname","pswd");
mysql_select_db("dbase", $db);
$user_email = mysql_real_escape_string($_GET['user_email']);
$result = mysql_query("SELECT * FROM tabname WHERE user_email='$user_email'", $db) or die(mysql_error());
$myrow = mysql_fetch_array($result);
echo $myrow["id"];
?>
  1. <?php
  2. $db = mysql_connect("localhost", "uname","pswd");
  3. mysql_select_db("dbase", $db);
  4. $user_email = mysql_real_escape_string($_GET['user_email']);
  5. $result = mysql_query("SELECT * FROM tabname WHERE user_email='$user_email'", $db) or die(mysql_error());
  6. $myrow = mysql_fetch_array($result);
  7. echo $myrow["id"];
  8. ?>
  • digisales
  • Newbie
  • Newbie
  • digisales
  • Posts: 9

Post 3+ Months Ago

Bingo no error at all - many, many thanks!
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8413
  • Loc: USA

Post 3+ Months Ago

digisales wrote:
Bingo no error at all - many, many thanks!

Alright... so I take it that it works. :)

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 88 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.