php 警告:mysql_result() 期望参数 1 是资源,给定的布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11423106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:29:21  来源:igfitidea点击:

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

phpmysql

提问by Mike Rinehart

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

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

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

我的 PHP 函数脚本昨晚运行良好,现在当我今天登录并处理它时,我得到了

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

“警告:mysql_result() 期望参数 1 是资源,布尔值给定”。

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 手册,我什至看过我所做的事情的使用和工作示例。任何人都可以帮我解决这个问题吗?我一直在修复一个又一个错误(当我今天登录时,很多东西都停止工作了),我在这里不知所措。如果有帮助,我将在 Windows 7 上为我的服务器使用 XAMPP。

Code: (also available via Pastebin)

代码:(也可通过 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;
}

?>

回答by DaveRandom

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

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

  1. You performed query that returns success/fail instead of a result set (e.g. UPDATE)
  2. Your query failed
  1. 您执行的查询返回成功/失败而不是结果集(例如UPDATE
  2. 您的查询失败

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.

在您的情况下,查询失败。它失败的原因是因为您已经在不需要的 PHP 字符串中转义了反引号。

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

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`