Pull info from MySQL database

Reply

Join Date: Dec 2008
Posts: 6
Reputation: redgem is an unknown quantity at this point 
Solved Threads: 0
redgem redgem is offline Offline
Newbie Poster

Pull info from MySQL database

 
0
  #1
Jan 22nd, 2009
I have a database that I want query and pull an users specific details into a "$profile" variable.

the url would be:

"www.website.com/profile.php?id=12345"

Thus, I use the $_REQUEST["id"] to put the id number into a variable and then I query the database using this id number.

The code is below:

<?php
/*OPEN THE DATABASE*/ 
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
/*PULL INFO FROM URL FOR GENERATION OF PUBLIC PROFILE*/
$idnum=$_REQUEST["id"];
/*PULL INFO FROM THE DATABASE*/
$pullprofile="SELECT * FROM 'accounts' WHERE (idnum = '$idnum')";
if(mysql_query($pullprofile,$con))
{
$profile[]=mysql_fetch_array($pullprofile,$con);
echo "Success loading profile!";
echo "Your id number is ".$profile['idnum'];
}
else
{
echo "Profile does not exist...";
}

/*CLOSE THE DATABASE*/ 

mysql_close($con); 

?>

For some reason the code keeps on returning the "Profile does not exist..." results, even though I know the profile does exist...

I'm new to PHP/SQL, so what am I doing wrong...?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 297
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: Pull info from MySQL database

 
0
  #2
Jan 22nd, 2009
  1. $idnum=$_GET['id'];

Use this..
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,102
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 139
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster

Re: Pull info from MySQL database

 
0
  #3
Jan 22nd, 2009
Originally Posted by redgem View Post
I have a database that I want query and pull an users specific details into a "$profile" variable.

the url would be:

"www.website.com/profile.php?id=12345"

Thus, I use the $_REQUEST["id"] to put the id number into a variable and then I query the database using this id number.

The code is below:

<?php
/*OPEN THE DATABASE*/ 
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
/*PULL INFO FROM URL FOR GENERATION OF PUBLIC PROFILE*/
$idnum=$_REQUEST["id"];
/*PULL INFO FROM THE DATABASE*/
$pullprofile="SELECT * FROM 'accounts' WHERE (idnum = '$idnum')";
if(mysql_query($pullprofile,$con))
{
$profile[]=mysql_fetch_array($pullprofile,$con);
echo "Success loading profile!";
echo "Your id number is ".$profile['idnum'];
}
else
{
echo "Profile does not exist...";
}

/*CLOSE THE DATABASE*/ 

mysql_close($con); 

?>

For some reason the code keeps on returning the "Profile does not exist..." results, even though I know the profile does exist...

I'm new to PHP/SQL, so what am I doing wrong...?
[OPTIONAL]
1. Using a raw id like this may be a bit dangerous. You should md5 it with a 'salt' or something, to protect your data access.
2. Try to avoid $_REQUEST whenever possible. Use $_GET for url parameters and $_POST for form data (general rule of thumb).
3. You need to clean and validate the incoming data (see posts elsewhere).
[/OPTIONAL]

4. Pass the cleaned variable to the query:

  1. $pullprofile="SELECT * FROM accounts WHERE idnum = '{$idnum}'";

Notice the change in syntax.

5. Run query and check for results:

  1. $result = mysql_query($pullprofile,$con));
  2. if(mysql_num_rows($result)>0){
  3. $profile = mysql_fetch_array($result);
  4. echo "Success loading profile";
  5. echo "Your id number is " . $profile['idnum'];
  6. }else{
  7. echo "Profile does not exist...";
  8. }
This should work. If this does not, check to see that the $_GET id really is in the DB.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 34
Reputation: jrdark13 is an unknown quantity at this point 
Solved Threads: 3
jrdark13 jrdark13 is offline Offline
Light Poster

Re: Pull info from MySQL database

 
0
  #4
Jan 22nd, 2009
I'm not entirely sure but wouldn't if(mysql_query($pullprofile,$con)) return true only if the query was accepted not if there was a row result?. That means that your query is generating an error probably due to the fact that you are requesting literally $idnum, use {$idnum} to make sure it goes through not as static but a variable. But that will still only tell you that the code went through, I think.

I would rather use something like this:
  1. $pullprofile = "SELECT * FROM accounts WHERE idnum = '".$idnum."'";//I like escaping
  2. $query = mysql_query($pullprofile,$con);
  3.  
  4. if(mysql_num_rows($query) > 0)
  5. {
  6. $profile[]=mysql_fetch_array($query);
  7. echo "Success loading profile!";
  8. echo "Your id number is ".$profile['idnum'];
  9. }
  10. else
  11. {
  12. echo "Profile does not exist...";
  13. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 952 | Replies: 3
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC