-1

I'm trying to get my head around this problem and after looking at all the other duplicates of this question, my question still isn't answered.

I'm a beginner at PHP but I'm pretty sure my code is fine... but obviously not.

Any help please?

My code is the following...

// Get some site-wide variables from the database first
$settings = mysql_query("SELECT * FROM cms_settings");

// Put the site settings into usable variables
while($row = mysql_fetch_array($settings)) {
    $site_name = $row['name'];
    $site_desc = $row['description'];
    $site_status = $row['status'];
}
Fractaliste
  • 5,777
  • 11
  • 42
  • 86
Dan
  • 152
  • 2
  • 14
  • 2
    You shouldn't use functions from [this extension](http://php.net/manual/fr/function.mysql-query.php), it's depreciated – Fractaliste Aug 20 '14 at 08:41
  • 1
    I'm assuming you mean "PHP expects parameter 1 to be of type resource, but boolean given"? This indicates an error in your SQL query, use `echo mysql_error();` to debug. you've also not posted a connection(and please make sure to remove any passwords before trying). – scragar Aug 20 '14 at 08:43
  • 1
    possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource (or mysqli\_result), boolean given](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole) – Marty Aug 20 '14 at 08:44
  • I'm sorry, I'm a beginner in PHP and new to Stack Overflow. I made a mistake guys... I added in or die with the mysql error included and it came back with "No database selected". Also Marty is right with his comment above. – Dan Aug 20 '14 at 08:47
  • @Dan `mysql_select_db('database');` but dude, don't concentrate on `mysql_`, you shouldn't learn the outdated stuff. Look on PHP.net how `PDO` works! – Daniel W. Aug 20 '14 at 08:48
  • Hi Dan, I tried PDO but it confused me way to much. I'm not sure how to include the select db thing though because added that in is causing more errors. – Dan Aug 20 '14 at 08:57

1 Answers1

4

Your query is failing, which means that the variable $settings is a boolean FALSE value. Try using this:

$settings = mysql_query("SELECT * FROM cms_settings");
if($settings === false){
    throw new Exception(mysql_error());
}

Judging from your comments above, you are not selecting your database before you attempt to run the query. Try:

mysql_select_db('your_database_name');

$settings = mysql_query("SELECT * FROM cms_settings");
if($settings === false){
    throw new Exception(mysql_error());
}
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
  • Hi Wayne, I'm getting the following now... `mysqli_select_db("db_cms");` `$settings = mysqli_query("SELECT * FROM cms_settings") or die($myQuery."

    ".mysqli_error());`
    – Dan Aug 20 '14 at 08:53
  • I used `$database = mysql_select_db("mydatabase", $sql);` and it worked now. – Dan Aug 20 '14 at 09:31