Organizational Research By

Surprising Reserch Topic

warning mysql result expects parameter 1 to be resource boolean given


warning mysql result expects parameter 1 to be resource boolean given  using -'php,mysql'


  Possible Duplicate:
  PHP: Warning: sort() expects parameter 1 to be array, resource given   




My PHP functions script was working fine last night, and now when I logged on to work on it some more today, I am getting

"Warning: mysql_result() expects parameter 1 to be resource, boolean given".

I have -no- idea why this isn't working. I've read the PHP manual online, and I've even seen examples where what I did is used and works. Can anyone please help me out with this? I've been fixing bug after bug (so many things stopped working when I logged on today) and I'm at my wits end here. If it helps, I'm using XAMPP on Windows 7 for my server.

Code: (also available via Pastebin)

<?php

function dbConnect() {
$dbserver="127.0.0.1";
$dbuser="Mike";
$dbpassword="mike";
$dbname="devsite";

$con = mysql_connect($dbserver, $dbuser, $dbpassword);
mysql_select_db($dbname, $con);
}

function getSiteTitle() {


$siteTitle = mysql_result(mysql_query("SELECT \`siteTitle\` FROM siteSettings"), 0);
return $siteTitle;
}

function getSiteHeader(){

$siteHeader = mysql_result(mysql_query("SELECT \`siteHeader\` FROM siteSettings"), 0);
return $siteHeader;
}

function getBodyContent() {


$bodyContent = mysql_result(mysql_query("SELECT \`bodyContent\` FROM siteSettings"), 0);
return $bodyContent;
}

?>

    

asked Oct 13, 2015 by kinnari
0 votes
15 views



Related Hot Questions

1 Answer

0 votes

The problem is that mysql_query() is returning a boolean instead of a result resource. There are two reasons this can happen:

  1. You performed query that returns success/fail instead of a result set (e.g. UPDATE)
  2. Your query failed

In your case the query failed. The reason it failed is because you have escaped the back ticks in the PHP string where you did not need to.

Your lines look like this:

$siteTitle = mysql_result(mysql_query("SELECT \`siteTitle\` FROM siteSettings"), 0);

When they should simply be this:

$siteTitle = mysql_result(mysql_query("SELECT `siteTitle` FROM siteSettings"), 0);

Now, some side notes:

  • Don't write new code that uses the mysql_* functions. They are deprecated and will eventually be removed from PHP. Use MySQLi or PDO instead (I personally recommend PDO, YMMV)
  • Nesting database functions in this way is not a particularly good way to write your code. It is much better to check the errors explicitly after every function call.

For example:

$result = mysql_query("SELECT somecol FROM sometable");
if (!$result) {
  // Handle error here
}
// Now process the result
  • You should quote either all identifiers, or none, in your queries (preferably all). Quoting only some makes it harder to read.

E.g.

SELECT `siteTitle` FROM `siteSettings`
answered Oct 13, 2015 by gauravg.gwl

...