Jump to content

[SOLVED] "mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given"???


blurredvision

Recommended Posts

I've attached my code at the bottom.  It's giving this error when I'm creating the $homearray and $awayarray in the IF ELSE.  Can anyone immediately see what I'm doing wrong?

 

<?php
require_once('config.php');
error_reporting(E_ALL);

$season = $_GET['season'];
$week = $_GET['week'];
$home_gamertag_id = $_GET['home_gamertag_id'];
$away_gamertag_id = $_GET['away_gamertag_id'];

if (isset($home_gamertag_id)) {
$query1 = "SELECT * FROM hometeam_stats WHERE gamertag_id=$home_gamertag_id AND season=$season AND week=$week";
$query1run = mysqli_query($dbc, $query1);
$homearray = mysqli_fetch_array($query1run);

$gamestats_id = $homearray['gamestats_id'];

$query2 = "SELECT * FROM awayteam_stats WHERE gamestats_id=$gamestats_id";
$query2run = mysqli_query($dbc, $query2);
$awayarray = mysqli_fetch_array($query2run);

echo '<span class="standingsheader">' . $awayarray['team'] . ' at ' . $homearray['team'] . '</span><br /><br />';
echo '<table cellpadding="5" align="center">';
echo '<tr class="standingsheader"><td>Score</td><td width="5"> </td><td>' . $awayarray['score'] . '</td><td width="5"> </td><td>' . $homearray['score'] . '</td></tr>';
echo '<tr class="standingsheader"><td>First Downs</td><td width="5"> </td><td>' . $awayarray['first_downs'] . '</td><td width="5"> </td><td>' . $homearray['first_downs'] . '</td></tr>';
echo '</table>';

} elseif (isset($away_gamertag_id)) {
$query1 = "SELECT * FROM awayteam_stats WHERE gamertag_id=$home_gamertag_id AND season=$season AND week=$week";
$query1run = mysqli_query($dbc, $query1);
$awayarray = mysqli_fetch_array($query1run, MYSQLI_ASSOC);

$gamestats_id = $awayarray['gamestats_id'];

$query2 = "SELECT * FROM hometeam_stats WHERE gamestats_id=$gamestats_id";
$query2run = mysqli_query($dbc, $query2);
$homearray = mysqli_fetch_array($query2run, MYSQLI_ASSOC);

echo '<span class="standingsheader">' . $awayarray['team'] . ' at ' . $homearray['team'] . '</span><br /><br />';
echo '<table cellpadding="5" align="center">';
echo '<tr class="standingsheader"><td>Score</td><td width="5"> </td><td>' . $awayarray['score'] . '</td><td width="5"> </td><td>' . $homearray['score'] . '</td></tr>';
echo '<tr class="standingsheader"><td>First Downs</td><td width="5"> </td><td>' . $awayarray['first_downs'] . '</td><td width="5"> </td><td>' . $homearray['first_downs'] . '</td></tr>';
echo '</table>';
}
?>

Your query failed (usually because of a syntax error in the query string or accessing a nonexistent table or column name or due to a typo) and returned a FALSE value instead of a result resource.

 

From the mysqli_query section of the php manual -

 

Return Values

Returns TRUE on success or FALSE on failure. For SELECT, SHOW, DESCRIBE or EXPLAIN mysqli_query() will return a result object.

 

And your code did not check if the query worked before blindly executing a mysqli_fetch_array() statement against the result resource.

 

Database code must always check to see if a query worked before continuing and accessing data returned by that query.

 

Making use of mysqli_error() will tell you what problem occurred. There is an example in the php manual in the mysqli_query() section.

You're probably going to get tired of preaching to me about error reporting by the time I get done with my site :).  This is the third time you've posted in a thread of mine over the past couple of days, and I appreciate it a lot.

 

So here's what I did with my code (I'm only showing one part of the IF ELSE now, since both are practically the same):

 

$query1 = "SELECT * FROM awayteam_stats WHERE gamertag_id=$home_gamertag_id AND season=$season AND week=$week";
$query1run = mysqli_query($dbc, $query1);
if ($query1run) {
	$awayarray = mysqli_fetch_array($query1run, MYSQLI_ASSOC);

	$gamestats_id = $awayarray['gamestats_id'];

	$query2 = "SELECT * FROM hometeam_stats WHERE gamestats_id=$gamestats_id";
	$query2run = mysqli_query($dbc, $query2);
	if ($query2run) {
		$homearray = mysqli_fetch_array($query2run, MYSQLI_ASSOC);

		echo '<span class="standingsheader">' . $awayarray['team'] . ' at ' . $homearray['team'] . '</span><br /><br />';
		echo '<table cellpadding="5" align="center">';
		echo '<tr class="standingsheader"><td>Score</td><td width="5"> </td><td>' . $awayarray['score'] . '</td><td width="5"> </td><td>' . $homearray['score'] . '</td></tr>';
		echo '<tr class="standingsheader"><td>First Downs</td><td width="5"> </td><td>' . $awayarray['first_downs'] . '</td><td width="5"> </td><td>' . $homearray['first_downs'] . '</td></tr>';
		echo '</table>';
	} else {
		echo mysqli_error($dbc);
	}
} else {
	echo mysqli_error($dbc);
}

 

And the error I get is:

 

"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 'AND season=1 AND week=2' at line 1"

 

My syntax looks correct to me, in fact, I've already used many queries that look just like this on other pages.

It looks messy and could cause problems...

 

$query1 = "SELECT * FROM `awayteam_stats` WHERE `gamertag_id`='{$home_gamertag_id}' AND `season`='{$season}' AND `week`='{$week}'";

 

Also ensure that $home_gamertag_id has something in it. :)

If you echo the query string, you will probably see what is wrong with it ($home_gamertag_id is probably empty resulting in - WHERE gamertag_id= AND...)

 

@ProjectFear, back-ticks are mysql specific syntax and should be avoided, not used unconditionally as they make moving your application to a different type of sql database difficult (or will be a problem should mysql more closely follow sql standards in the future.) Adding {} would help if a php syntax error was occurring due to an array variable being used inside the string, but that is not the case.

If you echo the query string, you will probably see what is wrong with it ($home_gamertag_id is probably empty resulting in - WHERE gamertag_id= AND...)

 

Yep, that's what is happening, it's not getting the $home_gamertag_id.  I don't understand why, though, since my URL shows the following (with my actual site taken out):

 

http://*.*.com/index.php?view_gamestats=y&season=1&week=2&home_gamertag_id=1

 

It's passing all 3 values in the URL, but I can only seem to get it to see the season and week.

@ProjectFear, back-ticks are mysql specific syntax and should be avoided, not used unconditionally as they make moving your application to a different type of sql database difficult (or will be a problem should mysql more closely follow sql standards in the future.) Adding {} would help if a php syntax error was occurring due to an array variable being used inside the string, but that is not the case.

 

I always thought you should use back-ticks.

$home_gamertag_id is either being overwritten or it is not being set.

 

I'll assume you have something like $home_gamertag_id  = $_GET['home_gamertag_id']; ? If not, you need to do that. If so, than that would mean that something in your code is overwriting it (usually using a single = in a comparison instead of two == )

I'll assume you have something like $home_gamertag_id  = $_GET['home_gamertag_id']; ?

 

I do, for both the home_gamertag_id and away_gamertag_id, whichever one gets passed based on the link that is created.  I did just find the problem though, and am now getting the entire query, but it still isn't working even though all my variables are now being read properly:

 

SELECT * FROM hometeam_stats WHERE gamertag_id=5 AND season=1 AND week=2

 

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

Actually, now that I look at it, I've completely overlooked this part.  It actually is putting a 12 on the front of my query, which before when I've pasted this, I thought it was attached to another PHP notice listed above it that doesn't have to do with the problem at hand.....what is that?

 

12SELECT * FROM awayteam_stats WHERE awayteam_stats.gamertag_id=1 AND season=1 AND week=2

 

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

I suspect the error is occurring in one of the other queries.

 

The near '' (two single-quotes with nothing between them) is generally due to an empty value in a query.

 

The 12 is probably being echoed by something in your code prior to the line that is echoing the mysqli error.

I suspect the error is occurring in one of the other queries.

 

The near '' (two single-quotes with nothing between them) is generally due to an empty value in a query.

 

The 12 is probably being echoed by something in your code prior to the line that is echoing the mysqli error.

 

Ya, the 12 was some debug that I put in there a few minutes ago that I forgot to take back out (it was the season # and week # ;)).

I suspect the error is occurring in one of the other queries.

 

The near '' (two single-quotes with nothing between them) is generally due to an empty value in a query

 

So what do you mean "one of the other queries"?  There are only 2 queries in this script that would be run, and it's not getting to the second one because the first one is failing.  The config.php file that's required at the very top works fine for all my other pages that have similar queries. ???

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.