Jump to content
Updating IPB tonight Read more... ×

Archived

This topic is now archived and is closed to further replies.

tribal

assigning query results to variables

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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;

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.