Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to fix Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given ?

online printing

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1 Alejandro

Alejandro

    CC Lurker

  • New Member
  • Pip
  • 9 posts

Posted 21 November 2014 - 04:10 AM

I'm making a private messaging system.
But I got the error message just like this again an again.

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\post\includes\pm_send.php on line 12

pm_send.php code is here
 

<?php

session_start();
require_once "connect.php";

$sql = "SELECT id, email FROM users WHERE showing='1'";
$result = mysql_query($sql);

$options = "";
$email = $_SESSION['email'];

while ($row=mysql_fetch_array($result)){
$USERid = $row['id'];
$USERname = $row['email'];
$option.="<OPTION VALUE=\"$USERid\">".$USERname."</OPTION>";
}
?>

How to fix it?


Edited by Roger, 22 November 2014 - 08:36 AM.


#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 21 November 2014 - 05:07 AM

What's the data type for showing?


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Blimp

Blimp

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 199 posts

Posted 21 November 2014 - 08:10 AM

Being fairly proficient in PHP, it's sad to admit that I'm stuck on this. However, it's most likely that the query isn't returning any information. Will await for the response to panthers question.



#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 21 November 2014 - 08:47 AM

hang on, line 12 appears to be this: while ($row=mysql_fetch_array($result)){

 

That sort of implies that there may have been an error of some sort. Doesn't $result evaulate to false if there were no results, or some other error occurred? Normally you'd wrap your logic in something like "if ($result) {"


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 21 November 2014 - 11:33 AM

One thing for sure is you need to check $result for errors before using it.

 

Then, the error you are getting might an SQL error, meaning you didn't connect, or didn't go to the right database, or your query is wrong.

In that last regard I think your showing=1 might not need the one to be surrounded by ' '



#6 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts

Posted 21 November 2014 - 03:00 PM

mysql_query must be used after connecting to the database, it will also return FALSE on error, or TRUE on non-returning statements such as INSERT/DELETE/UPDATE if the query is successfully completed. On a SELECT statement, it will always return a resource object on success (even if there are no rows) or FALSE on failure. mysql_num_rows($resource) will return 0 if there are no rows found, rather than an error.

 

As a programmer you must look up the documentation for each and every function you use, not only to find its proper use but to learn of each type of data it may accept or each error it may return and how to handle such an error, and any related support functions. Look at php.net.

 

My guess is that mysql_error() would reveal after that call something relating to the database not being selected (is it doing so succesfully in connect.php?), the table users not existing, or a field named wrong, or any related permissions.

 

Please note too that mysql_* is deprecated and will be removed in future versions of PHP, try to use the superceding and compatible mysqli extensions, or PDO for something more robust.

 

Alexander.


Edited by Alexander, 21 November 2014 - 03:04 PM.

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download