Welcome Guest ( Log In | Register )




                Web Hosting

 
Closed TopicNew Topic
Php Mysql Errors, Fetching arrays
FirefoxRocks
post Apr 21 2007, 06:06 PM
Post #1


Super Member
Group Icon

Group: [HOSTED]
Posts: 774
Joined: 12-July 06
From: Ontario, Canada
Member No.: 14,464
myCENTs:42.53


I am deciding to make a Multiplayer Online RPG type game. I will be building it off of PHP and MySQL to ensure makimum compatibility with Astahost's services (and it makes it easier wink.gif). I have a database setup with 1 table to hold user data and I have the login system setup properly as well as the registration form (obviously).

All games of course have something similar to gold, units and points. Because this is a turn-based game, I have turns.

Now for the problem:
I am trying to echo the points, gold and turns to the main page of the game. I tried using mysql_result, mysql_fetch_array and also mysql_fetch_assoc. It doesn't work. Here is my code:
CODE
<?php
require("db_connect.php");
$username = $_COOKIE['xtech_user'];
$zbits = mysql_query("SELECT `zbits` FROM `players` WHERE `username`=$username");
while($row1 = mysql_fetch_array($zbits))
  {
  echo "zBits: " . $row1['zbits'] . " | ";
  }
$turns = mysql_query("SELECT `turns` FROM `players` WHERE `username`=$username");
while($row2 = mysql_fetch_array($turns))
  {
  echo "zTurns: " . $row2['turns'] . " | ";
  }
$zpoints = mysql_query("SELECT `zpoints` FROM `players` WHERE `username`=$username");
while($row3 = mysql_fetch_array($zpoints))
  {
  echo "zPoints: " . $row3['zpoints'] . " ";
  }
mysql_close();
?>


The required file is the database connection file, it is just a $con variable along with mysql_connect and mysql_select_db("gameDatabase",'$con'). I use that for everypage that requires a MySQL connection.
I store the username in a cookie, so therefore I will retrieve the cookie value and put it in the variable $username.
Now I want to get a column (field) value from the players table where the username is what that was declared in the cookie variable.

When I run the mysql_query in the mysql_fetch_array, this error occurs:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/portal/public_html/game/head.php on line 13
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/portal/public_html/game/head.php on line 18
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/portal/public_html/game/head.php on line 23

I don't know what is causing this! I tried using SELECT * also, but the same error comes up.

I would like to get this working soon. Please help with this.
Go to the top of the page
 
+Quote Post
pyost
post Apr 21 2007, 09:16 PM
Post #2


Nenad Bozidarevic
Group Icon

Group: [MODERATOR]
Posts: 1,053
Joined: 7-November 05
From: Belgrade, Serbia
Member No.: 9,500
myCENTs:36.94


Try adding single quotes around $username, because it is a string:

$query = "SELECT `zbits` FROM `players` WHERE `username`='$username'";

I am not sure if this is necessary, but as far as I can see, that is the only thing I would do in a different way.
Go to the top of the page
 
+Quote Post
FirefoxRocks
post Apr 21 2007, 11:28 PM
Post #3


Super Member
Group Icon

Group: [HOSTED]
Posts: 774
Joined: 12-July 06
From: Ontario, Canada
Member No.: 14,464
myCENTs:42.53


Thank you! That automatically made it work! I thought variable names didn't need to be enclosed in quotes, but I guess it is necessary when you have them in MySQL functions. Now I need to add about 30-50 some more fields! sad.gif

Topic solved.
Go to the top of the page
 
+Quote Post

Closed TopicNew Topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts 3 FirefoxRocks 155 24th December 2008 - 01:43 PM
Last post by: Quatrux
No New Posts   8 nakulgupta 2,038 4th December 2008 - 05:26 AM
Last post by: iG-tohir
No New Posts   2 magiccode9 164 17th November 2008 - 12:05 PM
Last post by: magiccode9
No New Posts   5 lonebyrd 1,553 29th October 2008 - 11:00 PM
Last post by: Guest
No New Posts   11 rckstr4lfe 3,620 26th October 2008 - 03:54 PM
Last post by: Guest
No New Posts   3 khalilov 372 8th October 2008 - 05:27 AM
Last post by: TavoxPeru
No New Posts   9 anzoone 2,452 11th September 2008 - 06:56 AM
Last post by: Guest
No New Posts  
5 PHP
10 Houdini 1,802 6th September 2008 - 07:02 AM
Last post by: Guest
No New Posts   11 Alexandre Cisneiros 2,121 4th September 2008 - 06:45 AM
Last post by: Guest
No New Posts   4 wanhafizi 1,413 28th August 2008 - 04:53 AM
Last post by: Guest
No New Posts   18 lonebyrd 2,788 18th August 2008 - 09:41 PM
Last post by: Guest
No New Posts   4 aqaing 868 10th August 2008 - 09:06 AM
Last post by: TavoxPeru
No New Posts   6 demolaynyc 961 7th August 2008 - 01:57 PM
Last post by: Guest
No New Posts   8 warriors 4,485 6th August 2008 - 09:00 AM
Last post by: Guest
No New Posts   1 darkken 844 31st July 2008 - 08:35 PM
Last post by: Darasen