TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-19-2008, 10:33 PM   #1 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 9
Thanks: 1
NathanH is on a distinguished road
Default Newbie Horror

Okay, i'm guessing this is either not going to be possible or i'm just very stupid and have overlooked it. Probably the latter, but i'm learning :P

I've been trying to (it's going to sound easy) get a value out of the database and into a table cell. I can do that, but I need to take a member specific value out. As in, I can take out a value, but i can only show all values or only one, and it would be the same for everyone.

Maybe i'm not clear enough - i'm trying! :)

I'll give you the scenario:
- I have one table: 'users',
- I have 4 fields: 'ID', 'username', 'password', 'gold',
- I am trying to paste the member's gold on a page.

My code:
Code:
$showgold = "SELECT * FROM users";
$query = mysql_query($showgold);
while($row = mysql_fetch_array($query))
{
echo "<table>";
echo "<tr>";
echo "<td>" . $row['gold'] . "</td>";
echo "</tr>";
echo "</table>";
}
This shows me every member's amount of gold. What i'm trying to have is just the member seeing their amount of gold. I've been looking for an answer more or less all day. Maybe you guys/girls could help? :)

Thanks
NathanH is offline  
Reply With Quote
Old 02-19-2008, 10:47 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Plugin/Addon

First and foremost, welcome to the community, Nathan! I hope you stick around and mingle!

As for your little problem, you will be wanting to add a WHERE clause to your SQL query. The WHERE clause is synonymous with -- I want only these rows! The way you would do that is, as you have an ID in your table, every member has a unique member ID -- or at least should have if that row is the primary key, as well as unique and, optionally, auto_increment.

Let's assume that the user's ID is stored in a session variable called id. You could output the ID in your PHP code like so:

php Code:
var_dump($_SESSION['id']);

Now, to add the WHERE clause, and only pull back the one record for the unique member ID, you would construct your query to look like the following, considering my member ID is 5:

sql Code:
SELECT
    gold
FROM
    users
WHERE
    id = 5

Thus, if the ID is in $_SESSION['id'] then I have everything I possibly need to build that query and execute it:

php Code:
$szSQL = sprintf("SELECT gold FROM users WHERE id = %d", $_SESSION['id']);

If the sprintf function is somewhat alien to you, and I understand it might be -- we've all come from the ground upwards! Then may I recommend a quick read of not only the documentation page on the wonderful sprintf, but also an article right here at TalkPHP:

I hope this helps you! Incidentally, is this for a game? Do we good folks get a sneak preview?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-19-2008, 10:49 PM   #3 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

This should do the trick:

PHP Code:
$szQuery 'SELECT gold FROM users WHERE id='.$iMemberId.' LIMIT 1;
$pResult = mysql_query($szQuery);

$aRow = mysql_fetch_array($pResult); 
HTML Code:
<table>
<tr>
    <td><?php echo $aRow['gold']; ?></td>
</tr>
</table>
Where $iMemberId is the id of the user that is logged in.

PS. There's no need to do a while loop, since you're only fetching one row.
maZtah is offline  
Reply With Quote
Old 02-19-2008, 10:57 PM   #4 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 9
Thanks: 1
NathanH is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
As for your little problem, you will be wanting to add a WHERE clause to your SQL query. The WHERE clause is synonymous with -- I want only these rows! The way you would do that is, as you have an ID in your table, every member has a unique member ID -- or at least should have if that row is the primary key, as well as unique and, optionally, auto_increment.

I hope this helps you! Incidentally, is this for a game? Do we good folks get a sneak preview?
I had tried the WHERE clause, but was missing the SESSION variable. I remember thinking about whether to use SESSIONs or not, but as I'm trying to get to grips with basic stuff I made the mistake of missing it out! :P

I will be going to bed now, but will definately read up on sprintf when I'm next back. Thank you for the help, the tips and the heads-up! :)

I told myself I would learn on the structure of a game, as it provides me with (what I hope) is mainly what I will be using with php & mysql. General register/login/logout, general account stuff and nothing too complicated (I say complicated after this embarrasment :P).

And as for a sneak preview- there really is nothing, my design is somewhat limited and because I'm new(ish) there's no functionality :)
NathanH is offline  
Reply With Quote
Old 02-19-2008, 11:00 PM   #5 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 9
Thanks: 1
NathanH is on a distinguished road
Default

Quote:
Originally Posted by maZtah View Post
This should do the trick:

PHP Code:
$szQuery 'SELECT gold FROM users WHERE id='.$iMemberId.' LIMIT 1;
$pResult = mysql_query($szQuery);

$aRow = mysql_fetch_array($pResult); 
HTML Code:
<table>
<tr>
    <td><?php echo $aRow['gold']; ?></td>
</tr>
</table>
Where $iMemberId is the id of the user that is logged in.

PS. There's no need to do a while loop, since you're only fetching one row.
Thank you very much maZtah :) I'll get onto that right when I wake up :)
NathanH is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:13 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design