Jump to content


Photo

assigning query results to variables


  • Please log in to reply
4 replies to this topic

#1 tribal

tribal
  • Members
  • Pip
  • Newbie
  • 4 posts
  • LocationNYC

Posted 01 September 2003 - 10:03 AM

hi guys got a quick ??

i have a database with 4 columns: username, real_name, age and sex

the user logs into the site and does an automatic query of his information

i need to display to the user an output report which looks something like this:

Welcome <? $username ?> //username variable comes from login session

Your real name is <? $real_name ?>
Your age is <? $age ?>
etc..

all these variables are from the database query // Select * from users where username = $username

how do i assign the query results to the output variables ? thanks

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 01 September 2003 - 10:25 AM

if youre using

$query = \"Select * from users where username = \'$username\'\";

go like this:

$info = mysql_fetch_array(mysql_query($query)) or die(mysql_error());
$username = $info[\'username\'];
$real_name = $info[\'real_name\'];
$age = $info[\'age\'];
$sex = $info[\'sex\'];

the row that you select goes into the $info array, which you can then split up into various variables.

#3 tribal

tribal
  • Members
  • Pip
  • Newbie
  • 4 posts
  • LocationNYC

Posted 01 September 2003 - 10:26 AM

great! ill try that, thank you!

#4 tribal

tribal
  • Members
  • Pip
  • Newbie
  • 4 posts
  • LocationNYC

Posted 01 September 2003 - 10:33 AM

one more question, so if i want to display real name for example after doing the query, i would do this?


$real_name = $info[\'real_name\']
echo \"your real name is:\" $real_name;

#5 Dissonance

Dissonance
  • Members
  • PipPip
  • Member
  • 12 posts
  • LocationChicago, IL

Posted 01 September 2003 - 11:34 AM

You got it. Your echo statement is a little off, though:

[php:1:753995bc06]echo \"Your real name is: $real_name\";[/php:1:753995bc06]

When using double quotes, you can include variables in your echos without having to worry about running into any parsing errors (for the most part, anyway).
Friends don't let friends vote Republican.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users