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

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

问题描述

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

昨晚我的 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.

你的线条看起来像这样:

Your lines look like this:

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

什么时候应该是这样:

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

现在,一些旁注:

  • 不要编写使用 mysql_* 函数的新代码.它们已被弃用,最终将从 PHP 中删除.使用 MySQLiPDO(我个人推荐 PDO,YMMV)
  • 以这种方式嵌套数据库函数并不是编写代码的特别好方法.最好在每次函数调用后显式检查错误.
  • 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天全站免登陆