11 Replies - 7818 Views - Last Post: 02 August 2012 - 06:13 AM Rate Topic: -----

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Posted 01 August 2012 - 08:40 PM

Why do I keep getting the warning?

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in...


$dg_result = mysql_query("select * from mbr_members where screen_name = "+trim( $_POST['user_login']));
if( empty($dg_result) ) $errors->add('invalid_email');
$dg_row = mysql_fetch_array($dg_result);


i thought mysql_query returns resource...?

This post has been edited by Atli: 01 August 2012 - 09:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2858
  • View blog
  • Posts: 10,962
  • Joined: 15-July 08

Re: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Posted 01 August 2012 - 08:45 PM

No, it returns a result. You have to make parameter 1 be the db handler that you are querying and parameter 2 needs to be the query. Also, the String concatenation operator is the full stop: ".", so you might use that as well.

If you can though, I'd recommend using the OO version of MySQL. That way, the handler is a little bit more friendly.
Was This Post Helpful? 0
  • +
  • -

#3 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Posted 01 August 2012 - 08:53 PM

i confused myself with the concatenater in php and mysql.. thanks.

however, i basically copy the query from http://www.w3schools...ysql_select.asp where $result was not declared to be any type.. also here: http://www.w3schools...fetch_array.asp

they both take in just a query? whats the difference between their code and mine?
Was This Post Helpful? 0
  • +
  • -

#4 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Posted 01 August 2012 - 09:08 PM

mysql_select_db("betwinsh_mbr",$mbrdb);
$dg_result = mysql_query("select * from mbr_members where screen_name = "+trim( $_POST['user_login']));
if( empty($dg_result) ) $errors->add('invalid_email');
$dg_row = mysql_fetch_array($dg_result);
$dg_row = mysql_fetch_array($mbrdb,$dg_result);


now it says: Warning: mysql_fetch_array(): supplied resource is not a valid MySQL result resource?

This post has been edited by Atli: 01 August 2012 - 09:09 PM
Reason for edit:: Please use [code] tags when posting code.

Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,759
  • Joined: 08-June 10

Re: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Posted 01 August 2012 - 09:09 PM

Dogstopper, if I'm not mistaken, you are talking about the Improved MySQL Extension, whereas g37752 is following W3School in using the old, deprecated MySQL Extension.

@g37752. Following the W3Schools tutorial on PHP/MySQL is not a good idea. It's written using outdated functions, and even for an outdated tutorial, it's rather poorly written. I would recommend learning either the Improved MySQL Extension (usually refereed to as MySQLi) or the PDO extension.

As it happens, we have tutorials on both here on DIC:
- Introduction to Mysql Improved (mysqli) and Prepared Statements
- Introduction to PDO

Personally I'd recommend PDO, as it's not only limited to MySQL. But if you are not familiar with OOP yet, the MySQLi extension offers a procedural API as well, very much like the old MySQL functions.
Was This Post Helpful? 2
  • +
  • -

#6 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Posted 01 August 2012 - 09:09 PM

sorry, i used $dg_row = mysql_fetch_array($mbrdb,$dg_result);
Was This Post Helpful? 0
  • +
  • -

#7 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Posted 01 August 2012 - 09:56 PM

I found a good explanation for the warning message here: http://forums.devart...mysql-3251.html

so basically a query error causes $result to become a boolean. however, i cannot figure out how to make the query to work:

$dg_result = mysql_query("select * from mbr_members where screen_name=".trim($_POST['user_login'])); with or w/o trim(), with or w/o "."???
Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,759
  • Joined: 08-June 10

Re: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Posted 01 August 2012 - 10:07 PM

The first step to fixing the query would be to actually look at the query. Instead of compiling the query string inside the mysql_query call, compile it into a variable that is then passed to the query. That way you can echo the query to see what the problem is.
// Secure the user input. NEVER, EVER skip this
// by putting the user input directly into the SQL
// query, or you open yourself up to SQL Injection attacks.
$value = mysql_real_escape_string($_POST["user_input");

// Compile the query
$sql = "SELECT stuff FROM whatever
        WHERE something = {$value}";

// Execute the query
$result = mysql_query($sql);

// Test to see if it succeeded.
if ($result) {
    // It was successful, continue doing whatever
    // it is you wanted to do with the data.
}
else {
    // There was an error.
    echo "<p><b>Error!</b> Query failed: " . mysql_error() . "</p>";

    // And, for debugging purposes, show the query 
    // as well. Remember to remove this before making
    // the site public!
    echo "<pre>{$sql}</pre>";
}


That's basically how all your queries should be executed. The two most important things there are the mysql_real_escape_string() call, which should be done on EVER SINGLE USER INPUT you plan on using in the query (NO EXCEPTIONS!), and the IF statement used to verify the $result before it is used by functions like mysql_fetch_array().


By the way, using MySQLi and/or PDO prepared statements, you avoid the whole mysql_real_esacep_string() SQL Injection mess. It's one of the reasons why people should be abandoning the old mysql_query family of functions.

This post has been edited by Atli: 01 August 2012 - 10:09 PM

Was This Post Helpful? 2
  • +
  • -

#9 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Posted 01 August 2012 - 10:07 PM

so it's:
mysql_query("select * from mbr_members where screen_name='".trim($_POST['user_login'])."'");
Was This Post Helpful? 0
  • +
  • -

#10 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 239
  • View blog
  • Posts: 693
  • Joined: 24-November 08

Re: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Posted 02 August 2012 - 03:20 AM

I think you're missing the point of Atli's post. You are inserting raw, user supplied, data into your query. Even if you are just learning and this isn't going to be a public site, it's still better to get into the habit of making it secure.

You should have your value escaped before using it in your query, and have your query held in a separate variable before calling it. Read post #8 again, but this time read it thoroughly. Atli explained it well enough that it shouldn't need reiteration.
Was This Post Helpful? 2
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Posted 02 August 2012 - 05:27 AM

Why are you ignoring all suggestions to learn prepared statements? Since you clearly don't know mysql_* functions what's going to happen is this:
  • You will spend a good deal of time learning them.
  • You will then spend more time learning to scrub user data to protect them from sql injection attacks.
  • MySQL_* functions then disappear from PHP.
  • You will have to learn prepared statements anyway.


Why not take the easy way and just learn prepared statements?
Was This Post Helpful? 1
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2858
  • View blog
  • Posts: 10,962
  • Joined: 15-July 08

Re: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Posted 02 August 2012 - 06:13 AM

View PostAtli, on 02 August 2012 - 12:09 AM, said:

Dogstopper, if I'm not mistaken, you are talking about the Improved MySQL Extension, whereas g37752 is following W3School in using the old, deprecated MySQL Extension.


I apologize. I must have missed the missing "i" at the end. My bad.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1