Jump to content


Photo

mysql_fetch_array() expects parameter 1 to be resource, boolean give


  • Please log in to reply
6 replies to this topic

#1 CiszLaserna

CiszLaserna

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 03 February 2013 - 01:02 PM

Kinda new to php im using array to post the inbox of an email application but it seems i got an error this code here:


<?php

$hostname = "localhost"; 
$password = "password";
$database = "reports";
$username = "your_name";


//connection to the database
$con = mysql_connect($hostname, $username, $password)
 or die("Connecting to MySQL failed");


//select a database to work with
$selected = mysql_select_db("report", $con)
  or die("Could not select examples");

//execute the SQL query and return records
$result = mysql_query("SELECT id, stations, title, detail from insert");


//fetch tha data from the database 
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "ID:".$row{'Id'}. "Station:".$row{'Stations'}. "Title:".$row{'Title'}. "Detail:".$row{'Detail'}."<br>";
}

//close the connection
mysql_close($con);
?>


#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 03 February 2013 - 01:04 PM

You need to check for errors. See the link in my signature on debugging SQL.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 CiszLaserna

CiszLaserna

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 03 February 2013 - 01:15 PM

error is this

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\report\inbox.php on line 23

Edited by CiszLaserna, 03 February 2013 - 01:17 PM.


#4 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 03 February 2013 - 02:41 PM

You need to check for errors. See the link in my signature on debugging SQL.


My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#5 Andy123

Andy123

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts
  • LocationDenmark

Posted 03 February 2013 - 03:56 PM

If I had a penny for every time someone asked about this error on this forums, I would be rich. ;) I don't mean to be rude.

The error message is actually very precise and helpful. mysql_fetch_array() is being called with a boolean value where a resource is expected. If you look at the documentation for mysql_query(), you will see that it returns FALSE on failure, which is a boolean. This value is stored in $result, which is then passed to mysql_fetch_array(). So, an error occurs with your SQL query. Perhaps you made a spelling mistake or perhaps the word "insert" is not allowed; I am actually not sure. Anyways, you can check the error like this:

$result = mysql_query("SELECT id, stations, title, detail from insert") or die(mysql_error());

Also, please consider using MySQLi or PDO instead as the mysql_* extension is deprecated. I am merely name dropping here, so please take the time to read about these.
Blogging about PHP and other programming related subjects at CodingExplained.com.

#6 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 03 February 2013 - 04:12 PM

The problem is the use of "insert" but OP needs to learn to capture mysql errors.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#7 CiszLaserna

CiszLaserna

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 03 February 2013 - 04:31 PM

opps did na see. thank you :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com