Jump to content

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


CiszLaserna

Recommended Posts

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);
?> 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.