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>';
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

$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 == )

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 # ;)).

Link to comment
Share on other sites

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. ???

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.