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);
?>
mysql_*
functions, they are deprecated (see the red box). Switch to MySQLi or PDO. Andmysql_query
is apparently returningFALSE
. What does your database look like? – Marcel Korpel May 19 at 10:15$result = mysql_query($sql) or die(mysql_error());
. Also rember thatmysql_*
functions are deprecated, usemysqli
orPDO
instead. – Fabio May 19 at 10:18