...

View Full Version : mysql_fetch_array(): supplied argument is not a valid MySQL result resource



crazykid
12-06-2011, 11:52 AM
This is the error I get:


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/mounted-storage/home49a/sub006/sc33133-WBJC/rpgmax.com/wp-content/themes/gameup-single-pro/header-gameprofile.php</b> on line <b>21</b><br />
- Game Profile - RPGMAX</title>

This is the code in the file:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<!-- Title -->
<title>
<?php
/*
Connects to the database using the following settings:
Host: hidden
Username: hidden
Password: hidden
*/
$connection = mysql_connect("hidden","hidden","hidden");

//Selects the Games table
mysql_select_db("rpgmax", $connection);

$gameId = $_GET['gameId'];
$result = mysql_query("SELECT * FROM wp_rpgmax_games WHERE gameId=$gameId");
$row = mysql_fetch_array($result);
echo $row['gameName'];
?>
- <?php wp_title(''); ?> - <?php bloginfo('name'); ?></title>

This is the same code I use at MMOCraze.com

My code there is:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<head>
<!-- Title -->
<title>
<?php
/*
Connects to the database using the following settings:
Host: hidden
Username: hidden
Password: hidden
*/
$connection = mysql_connect("hidden","hidden","hidden");

//Selects the Games table
mysql_select_db("mmocraze", $connection);

$gameId = $_GET['gameId'];
$result = mysql_query("SELECT * FROM wp_MMOCraze_games WHERE gameId=$gameId");
$row = mysql_fetch_array($result);
echo $row['gameName'];
?>
- <?php wp_title(''); ?> - <?php bloginfo('name'); ?></title>

with no error in the file...

Any ideas with what's wrong with the code on RPGMax?

abduraooft
12-06-2011, 12:02 PM
$result = mysql_query("SELECT * FROM wp_MMOCraze_games WHERE gameId=$gameId");
You should add proper error checks to your query, like

$result = mysql_query("SELECT * FROM wp_MMOCraze_games WHERE gameId=$gameId") or die(mysql_error());

PS: Sanitise all external inputs before usingin queries to prevent sql injection. Read http://php.net/manual/en/security.database.sql-injection.php

crazykid
12-06-2011, 12:09 PM
Tried that and got this error message:


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

which makes no sense to me....

by the way, the mysql_fetch_array() error occurs on one site but NOT the other...BOTH sites are using the exact same mysql_fetch_array() code, which makes me wonder what the problem is...

abduraooft
12-06-2011, 12:18 PM
which makes no sense to me....Change
$result = mysql_query("SELECT * FROM wp_MMOCraze_games WHERE gameId=$gameId") or die(mysql_error()); to

$sql="SELECT * FROM wp_MMOCraze_games WHERE gameId=$gameId";
$result = mysql_query($sql) or die(mysql_error(). '<br>query: '.$sql); to get more clues.

Also add
print_r($_GET); at the top of your code to see the values inside $_GET array.

crazykid
12-06-2011, 12:36 PM
Error:


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1<br>query: SELECT * FROM wp_rpgmax_games WHERE gameId=

abduraooft
12-06-2011, 03:09 PM
query: SELECT * FROM wp_rpgmax_games WHERE gameId=
Isn't obvious now? That seems the variable $gameId (and $_POST['gameId']) is empty. Did you see the other diagnostic in my last post?

crazykid
12-06-2011, 04:23 PM
Error after adding the print_r code:


Array
(
)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1<br>query: SELECT * FROM wp_rpgmax_games WHERE gameId=

by the way, gameId is a variable in my database that HAS data in it. In my code, it matches up with its counterpart:


$gameId = $_GET['gameId'];


$sql="SELECT * FROM wp_rpgmax_games WHERE gameId=$gameId";

abduraooft
12-06-2011, 04:28 PM
Quote:
Array
(
)
Your $_GET array is empty. How does your variable $gameId get some value then? Could you post the url the page that you work on?

bdl
12-06-2011, 04:49 PM
Error after adding the print_r code:



by the way, gameId is a variable in my database that HAS data in it. In my code, it matches up with its counterpart:


$gameId = $_GET['gameId'];


$sql="SELECT * FROM wp_rpgmax_games WHERE gameId=$gameId";

Just to be clear here, `gameId` isn't a variable in your database, it's a field in a table (`wp_rpgmax_games`). The variables referenced are $gameId and $_GET['gameId'] (technically an array index).

While it's nice to have a consistent naming convention, you want to be aware there is a distinction between a variable in PHP and a field in MySQL. You can name the PHP variable `froozle` if you want (though I don't recommend it), it's still the data contained in the var and the target that matters.

Here's a tip regarding naming conventions that I like to use: name your HTML input fields something like "input_gameId" and then the corresponding $_POST (or $_GET) index will be $_POST['input_gameId']. This helps you differentiate between incoming, unsanitized and unvalidated data and the actual data you intend to interact with the database. Once it is has been verified / cleaned up, you can rename it "$clean_gameId" (or something similar).

crazykid
12-06-2011, 04:54 PM
http://rpgmax.com/mmo/gamedirectory/

Click on ANY of the links...they are coded to lead to a http://rpgmax.com/mmo/gamedirectory/gameprofile/?gameId=XXX page but they dont...they lead directly do a /gameprofile/ page.

Now go to http://mmocraze.com/game-directory and click on any of the links. They WORK...they lead to an actual game profile page with a gameId connected to the URL.

Both sites are hosted on the same hosting server, use same seo settings, use same files (with different database info but databases have same structure). RPGMax is the one that has the problems though.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum