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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.