extracting data from MySQL database

im fairly new to php and ive been searching the internet for help on this and have tried many different ways and yet i cant get it to work the way im wanting it, so i figure theres some basic principle ive missed.

i took my log in code from a site(that gave permission to take it) and managed to change it to let the user select a “profession” (which i spelled wrong then so i just kept it wrong) when registering. This works correctly but now i want to get that data and use it, just that one piece of data.

this is the piece of code im useing…

/* Username and password correct, register session variables */
$_POST[‘user’] = stripslashes($_POST[‘user’]);
$_SESSION[‘username’] = $_POST[‘user’];
$_SESSION[‘password’] = $md5pass;

 mysql_connect("my_db_host","db_user","db_pass");
 mysql_select_db("db_name");
 $queryprof = mysql_query("SELECT proffesion FROM users WHERE username = "[u]The Name They Typed In When Loging In[/u]"");
    while($row = mysql_fetch_array($queryprof))
     { $prof = $row['proffesion'];
      $prof = [u]$_SESSION['proffesion'];[/u]
     }

when connecting to the DB i have changed the names and such to be correct.
after this, to test to see if it’s worked im printing out the result in a separate function that shows when the user has logged in.

i THINK my problem lies in my variable choices, in the two underlined sections ive tried many different variables, none of which printed out.

or maybe i dont understand $_SESSION variables, or tables in databases weren’t meant to be used this way, or im missing some step to have the result print out in teh separate function.

i hope i provided the correct section/amount of code to help me out, thank yall
ill keep looking for answers.

In the mysql_query() function call, since your are using double quotes to surround sql query, you need to escape double quotes within sql expression, or just use single quotes, like this:

[php]
$queryprof = mysql_query(“SELECT proffesion FROM users WHERE username = ‘The Name They Typed In When Loging In’”);
[/php]

From logic of your code, it seem you want to read profession and store is in a session variable, right? If so, here is a mistake in your code (you’re reading profession from the db, but then you override that value by assigning value stored in $_SESSION[‘proffesion’] which is not set before?):

[php]
while($row = mysql_fetch_array($queryprof))
{ $prof = $row[‘proffesion’];
$prof = $_SESSION[‘proffesion’];
}
[/php]

Correct code would be:
[php]
while($row = mysql_fetch_array($queryprof))
{ $prof = $row[‘proffesion’];
$_SESSION[‘proffesion’]=$prof;
}
[/php]

thank you! its working perfectly now
and i think i understand extractign data a bit more now

Sponsor our Newsletter | Privacy Policy | Terms of Service