Jump to content

assigning query results to variables


tribal

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

Link to comment
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.

Link to comment
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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.