警告:mysql_result()期望参数1为资源,给定布尔值 [英] Warning: mysql_result() expects parameter 1 to be resource, boolean given

查看:53
本文介绍了警告:mysql_result()期望参数1为资源,给定布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
PHP:警告:sort()需要参数1成为数组,给定资源

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

我的PHP函数脚本昨晚运行良好,现在当我今天登录进行更多工作时,我得到

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

警告:mysql_result()期望参数1为资源,给定布尔值".

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

我不知道为什么这不起作用.我已经在线阅读了PHP手册,甚至还看到了使用和使用我所做的示例.谁能帮我这个忙吗?我一直在修正错误(现在登录后,很多事情停止了工作),而我的智慧到此为止.如果有帮助,我将在Windows 7上为服务器使用XAMPP.

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.

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

?>

推荐答案

问题是mysql_query()返回的是布尔值而不是结果资源.发生这种情况的原因有两个:

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

  1. 您执行的查询返回的是成功/失败,而不是结果集(例如UPDATE)
  2. 您的查询失败

在您的情况下,查询失败.失败的原因是因为您逃脱了不需要的PHP字符串中的反勾号.

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.

您的行如下所示:

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

什么时候应该这样:

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

现在,一些注意事项:

  • 不要编写使用mysql_*函数的新代码.它们已被弃用,并将最终从PHP中删除.使用 MySQLi
  • 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.

例如:

$result = mysql_query("SELECT somecol FROM sometable");
if (!$result) {
  // Handle error here
}
// Now process the result

  • 您应在查询中(最好是全部)引用所有标识符或不引用.仅引用一些内容将使其更难阅读.
  • 例如

    SELECT `siteTitle` FROM `siteSettings`
    

    这篇关于警告:mysql_result()期望参数1为资源,给定布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆