using php to return specific data from mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shadow_blade47
Forum Newbie
Posts: 12
Joined: Fri Nov 07, 2003 3:41 am

using php to return specific data from mysql

Post 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...
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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"];

?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

You're missing an offset/fieldname argument for mysql_result. Check out the manual for [php_man]mysql_result[/php_man].
Last edited by McGruff on Wed Aug 10, 2005 11:00 am, edited 1 time in total.
shadow_blade47
Forum Newbie
Posts: 12
Joined: Fri Nov 07, 2003 3:41 am

Post 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.
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post 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.
Post Reply