0

ATTENTION: I DO HAVE TO SAY THAT THERE ARE LOTS OF SIMILAR QUESTIONS. BUT I COULD NOT RESOLVE MY PROBLEM GOING THROUGH THE LISTS OF ANSWERS.


I am trying to select data from a table but get this error message:

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in /home/---/---/register.php on line 34

This is my code:

{       

$userQuery = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `$tableName`
 WHERE `$userNameField`='$userName'"));
if($userQuery[0] > 0){

    $message = 'This username already exists. Please select another.';

} else {.......
display-name-is-missing
  • 4,816
  • 5
  • 26
  • 41
Sammy7
  • 246
  • 5
  • 20
  • 1
    http://www.php.net/mysql_query --- http://php.net/mysql_fetch_row – Funk Forty Niner Feb 03 '14 at 22:15
  • See [this answer](http://stackoverflow.com/a/11674313/250259) for how to troubleshoot this. And don't nest mysql functions like that. It makes error handling impossible as you now know. – John Conde Feb 03 '14 at 22:16
  • This `fetch_me_dinner(at_the_local_hardware_store("SELECT` stands at getting better results than `mysql_fetch_row(mysql_query("SELECT` – Funk Forty Niner Feb 03 '14 at 22:24
  • @JohnConde I've looked at your link I've tried a lot nothing seems to work?!? You have any other advice? – Sammy7 Feb 04 '14 at 03:05

1 Answers1

0

Probably your query is failing. Try using something like:

   or die(mysql_error());

But keep in mind: you shouldn't nest your mysql functions like that...

JiFus
  • 939
  • 7
  • 17
  • where should I add that? right after this: '{ $userQuery = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `$tableName` WHERE `$userNameField`='$userName'"));' – Sammy7 Feb 03 '14 at 22:23
  • Message above for @JiFus – Sammy7 Feb 03 '14 at 22:24
  • It should be right after your query, so: mysql_query(blahblah) or die (mysql_error()); – JiFus Feb 03 '14 at 22:25
  • Hey thanks but it didn't help at all it just says the same exact error. :( – Sammy7 Feb 04 '14 at 03:03
  • Try to rewrite your code, so it's much more clear. Start with: $sql = "SELECT.... " then say: $query = mysql_query($sql) or die (mysql_error()) and then: $numberofrows = mysql_num_rows($query). The variable $numberofrows now holds the number of rows you got from your query. That is much better then nesting all those functions. – JiFus Feb 04 '14 at 08:09