Computing.Net > Forums > Database > PHP / MySQL Retrieve one record

PHP / MySQL Retrieve one record

Reply to Message Icon

Original Message
Name: suspect52732
Date: August 20, 2007 at 19:50:28 Pacific
Subject: PHP / MySQL Retrieve one record
OS: XP
CPU/Ram: 2.4/512
Comment:

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!


Report Offensive Message For Removal


Response Number 1
Name: Razor2.3
Date: August 20, 2007 at 20:31:48 Pacific
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 2
Name: suspect52732
Date: August 20, 2007 at 21:57:27 Pacific
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 3
Name: mie2com
Date: September 25, 2007 at 16:13:05 Pacific
Reply: (edit)

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 Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: PHP / MySQL Retrieve one record

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge