View Full Version : How do i assign a database record to a variable?

05-16-2008, 03:13 PM
Hi, how do i assign a database record to a variable in php?

after accessing mySQL and the table and taking in the result...

$result = mysql_query("SELECT * FROM user WHERE username='$bang'");

after this statement what do i do?
something like this?

while($row = mysql_fetch_array($result))
$sang = $row['username'];
$tang = $row['picture'];

I'm trying to use the include function in a member list data page. So each time a visitor visits with the username chosen, it will display a picture and the name of the person.

05-16-2008, 04:16 PM
If you're only getting one row from the database (you can ensure this by adding "LIMIT 1" to the end of your query), then you don't have to put it in a while loop. Instead just:

$result = mysql_query("SELECT * FROM user WHERE username='$bang'");

$row = mysql_fetch_array($result);
$sang = $row['username'];
$tang = $row['picture'];

Just make sure that if the value $bang is set by the user that you escape any illegal characters! This looks like a potential exploit! Before using $bang in your query use the php function addslashes or mysql_real_escape_string on it.

05-17-2008, 12:11 AM
Oh, thanks for the info. Ah, i'll do some research on mysql_real_escape_string.

Thanks for the information, really useful~

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum