PHP / MySQL Retrieve one record

August 20, 2007 at 19:50:28
Specs: XP, 2.4/512

I am tring to get one single cell from a database. The primary key is username. I want to store the single cell in a variable called $charity. The code below seems to work. However, this outputs to the screen the answer. I want to store the answer in a variable.

$query="SELECT charity FROM `users` WHERE username='$username'";
$result=mysql_query($query);
$row = mysql_fetch_assoc($result);
echo $row['charity'];


------------------


Any help would be greatly appreciated!


See More: PHP / MySQL Retrieve one record

Report •


#1
August 20, 2007 at 20:31:48

Um... What's wrong with replacing the echo line with:
$charity=$row['charity'];
? I only ask because my PHP experience is nonexistent.


Report •

#2
August 20, 2007 at 21:57:27

I tried every possible combination of that I can think of:

$charity=$row['charity'];
$charity=$row[charity];
$charity=$row["charity"];
$charity=$row['$charity'];
$charity=$row[$charity];
$charity=$row["$charity"];
$charity==$row['charity'];
$charity->$row['charity'];


But nothing seems to work. I know I am close though, because:

echo $row['charity'];

yields the right return. I just cant figure how to get it put into a variable.



Report •

#3
September 25, 2007 at 16:13:05

Hi. I'm still learning MySQL, i'm not sure of this method of solution, but it should work:

$query = mysql_query("SELECT charity FROM `users` WHERE username=`$username`");
while($result = mysql_fetch_object($query)) {
$charity = $result->FIELD_NAME_HERE;
}
print($charity);


Report •

Related Solutions


Ask Question