0

I have a PHP function that is giving me the following error message:

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\includes\stat.php on line 10.

The function works perfectly and displays everything, so I don't know why I'm getting this warning. I've been messing with it for an hour now with no luck.

My function code is as follows.

function getStat($stat,$name) {
include 'includes/config.php';
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
    or die ('Error connecting to mysql:');
mysql_select_db($dbname);
$query = sprintf("SELECT $stat FROM players WHERE username = '%s'",
mysql_real_escape_string($_SESSION['username']));
$result = mysql_query($query);
------>list($value) = mysql_fetch_row($result);<-------------------line throwing warning
return $value;      
}
  • WHY are you not checking for errors? This has failed: `$result = mysql_query($query);`, probably because you have a syntax error in your query. Check `mysql_error()`. –  Jan 11 '14 at 01:54
  • Not sure how it can `display everything and work fine` when your query fails. It can't. You are probably calling your function multiple times. – jeroen Jan 11 '14 at 01:55
  • Side note: Do not use `mysql_*()` functions. They are deprecated and will be removed. Use `mysqli_*()` or `PDO`. –  Jan 11 '14 at 01:56
  • I for one, can't see how/why you're using PHP's [`list()`](http://www.php.net/list) language construct, in conjunction with your query. "Unless", there's something I don't know about, *yet*. – Funk Forty Niner Jan 11 '14 at 02:00
  • Stop reading old outdated tutorials and how to's. Begin with http://www.phptherightway.com/#databases and go from there. – user555 Jan 11 '14 at 02:06

2 Answers2

2

This is again because wherever people are learning how to use MYSQL never tells you how to error check.

When mysql_query is called, it returns FALSE if there was a failure, or an object that mysql_fetch_row can use. If you fail to inspect the return value of mysql_query before handing it to mysql_fetch_row, you create a problem.

To error check it, you should do something like this:

$result = mysql_query("SELECT * FROM players");
if (!$result) {
    // Something went wrong
    die(mysql_error());
}

// If we got here, there was no error and we can fetch rows
$row = mysql_fetch_rows($result);

MOST IMPORTANTLY:

Reading the documentation is especially helpful. You will notice that it tells you that the use of mysql_ functions is very unwise, and you should convert to mysqli_ and PDO API's. See the PHP documentation, it also would have shown you what I just typed for you! :-)

See the big red box! http://us1.php.net/manual/en/function.mysql-query.php

Rottingham
  • 2,523
  • 1
  • 11
  • 14
0

Rottingham's solution is correct.

Additionally it might be worth mentioning, that warnings never cause a program to fail. You can suppress them by prepending an @ to the function name, so in your case that would be

$row = @mysql_fetch_rows($result);

But obviously, it is always better to fix whatever caused the problem.

Phil
  • 768
  • 3
  • 14
  • 2
    Thanks for the Kudos, so I hate to squash you, but the use of @ is the reason we have so many of these questions. Bad implementation of features that probably shouldn't have made it into the language. Silencing errors means he never would know if there was an error and have no way to trace an error to its origin – Rottingham Jan 11 '14 at 01:58