Page 1 of 1

using php to return specific data from mysql

Posted: Sun Nov 09, 2003 2:42 pm
by shadow_blade47
hey there!

I'm trying to return a piece of data from a MySQL database and print it on the screen using the echo command.

I want this to appear on the screen:

GOLD : 'then the number of gold, say 1254 etc.'

So this is the command i use to display that

<?php echo "Gold: $gold" ?>

That will work fine, but it's getting the data assigned to the variable gold in a readable format thats the problem. I've tried doing something like this:

$gold_query = "SELECT gold
FROM player
WHERE username = $_COOKIE['user_name']";

$gold_result = mysql_query($gold_query);
$gold = mysql_result($gold_result);

why doesn't that work? The cookie has already been set by the way, i just need to work out how to pull the data out of the databse and assign it to the variable $gold.

Thanks in advance...

Posted: Sun Nov 09, 2003 3:43 pm
by Gen-ik

Code: Select all

<?php

$gold_query = "SELECT `gold` 
FROM `player` 
WHERE `username` = '{$_COOKIE['user_name']}' ";

$gold_result = mysql_fetch_array(mysql_query($gold_query));
$gold = $gold_result["gold"];

?>

Posted: Sun Nov 09, 2003 5:05 pm
by McGruff
You're missing an offset/fieldname argument for mysql_result. Check out the manual for [php_man]mysql_result[/php_man].

Posted: Tue Nov 11, 2003 11:51 am
by shadow_blade47
I tried the following code:

Code: Select all

<?php 

$gold_query = "SELECT `gold` 
FROM `player` 
WHERE `username` = '{$_COOKIE['user_name']}' "; 

$gold_result = mysql_fetch_array(mysql_query($gold_query)); 
$gold = $gold_result["gold"]; 

?>
But it didn't work, help would be appreciated. All i want to do is get the exact number stored in that cell of the database and print it to the screen, cheers.

Posted: Tue Nov 11, 2003 12:33 pm
by Linkjames
You haven't told it that you are making a mysql query. Try this

Code: Select all

<?php 

$gold_query = mysql_query("SELECT `gold` 
FROM `player` 
WHERE `username` = '{$_COOKIE['user_name']}' "); 

$gold_result = mysql_fetch_array($gold_query); 
?>
Also, you don't need the $gold_result array as gold is the only item in the variable.