Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm still new to PHP and haven't developed the knowledge of knowing how to fix most bugs when I anticipate them. I have a problem with connecting to a database the I have just created. I'm learning this example from a book. The browser keeps returning the result: "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in: 'Path to the file'". I assume there's something wrong with $result, but please have a look at it. My code looks like this:

<?php

    // Open a MySQL connection
    $link = mysql_connect('xxx', 'xxx', 'xxx');
    if(!$link) {
        die('Connection failed' . mysql_error());
    }

    // Select the database to work with 
    $db = mysql_select_db('test');
    if(!$db) {
        die('Selected database unavailable: ' . mysql_error());
    }

    // Create and execute a MySQL query
    $sql = "SELECT artist_name FROM artists";
    $result = mysql_query($sql);

    // Loop through the returned data and output it
    while($row = mysql_fetch_array($result)) {
        printf("Artist: %s<br />", $row['artist_name']);
    }

    // Free the memory associated with the query 
    mysql_free_result($result);

    // Close the connection
    mysql_close($link);

?>
share|improve this question
2  
You use the old mysql_* functions, they are deprecated (see the red box). Switch to MySQLi or PDO. And mysql_query is apparently returning FALSE. What does your database look like? – Marcel Korpel May 19 at 10:15
Try to debug readin mysql_error: $result = mysql_query($sql) or die(mysql_error());. Also rember that mysql_* functions are deprecated, use mysqli or PDO instead. – Fabio May 19 at 10:18
Thank you Fabio – Mosire May 19 at 15:11
add comment (requires an account with 50 reputation)

2 Answers

In case mysql_query fails, it returns false, a boolean value. When you pass this to mysql_fetch_array function (which expects a mysql result object) we get this error. Make sure that the query which you are using will fetch some result or you are using correct column name.

You should switch to use mysqli or PDO as others are suggesting.

share|improve this answer
1  
The last sentence is not true: use prepared statements, which are available through the MySQLi and PDO interfaces. – Marcel Korpel May 19 at 10:23
@marcel Thanks for rectifying :) – Vivek Sadh May 19 at 10:26
You mean both Mysqli and PDO supports prepared statement ? – Vivek Sadh May 19 at 10:26
Yes, indeed, see bobby-tables.com/php.html – Marcel Korpel May 19 at 10:30
I read somewhere that mysqli does not support prepared statement. See the table here :- net.tutsplus.com/tutorials/php/… Is it wrong ? – Vivek Sadh May 19 at 10:32
show 2 more commentsadd comment (requires an account with 50 reputation)

There are no problems in your code and the problem is in your Query.

Replace

$result = mysql_query($sql);

With

$result = mysql_query($sql) or die(mysql_error());

And you will find what's wrong in your Query.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.