Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql_fetch_array()布尔错误,但没有说明?_Php_Mysql - Fatal编程技术网

Php mysql_fetch_array()布尔错误,但没有说明?

Php mysql_fetch_array()布尔错误,但没有说明?,php,mysql,Php,Mysql,可能重复: 我知道我们不应该问这个问题,因为以前有人问过这个问题,这里有一个答案。但我不认为这是一回事。我以前从来没有遇到过这样的布尔错误,它给出了错误所在行号的描述 取而代之的是,我收到的只是这个错误消息,没有任何行信息或其他信息来帮助我识别问题 下面是我逐字逐句听到的错误: : mysql_fetch_array() expects parameter 1 to be resource, boolean given in on line : mysql_fetch_array() expe

可能重复:

我知道我们不应该问这个问题,因为以前有人问过这个问题,这里有一个答案。但我不认为这是一回事。我以前从来没有遇到过这样的布尔错误,它给出了错误所在行号的描述

取而代之的是,我收到的只是这个错误消息,没有任何行信息或其他信息来帮助我识别问题

下面是我逐字逐句听到的错误:

: mysql_fetch_array() expects parameter 1 to be resource, boolean given in on line : mysql_fetch_array() expects parameter 1 to be resource, boolean given in on line
这很奇怪,因为它通常会显示行号,但在这种情况下显然不会

我正在使用这个mysql函数:

function account_perms() {
    global $connection;
    global $_SESSION;
    global $profile_id;
    $query = "SELECT *
        FROM ptb_permissions
        WHERE ptb_permissions.private_id = \"$profile_id\"
        AND ptb_permissions.user_id = ".$_SESSION['user_id']." ";

    $account_perms = mysql_query($query, $connection);
    confirm_query($query, $connection);
    return $account_perms;
}
// rest of code ..
var_dump($query);

$account_perms = mysql_query($query, $connection);

// etc..
下面是php代码:

<?php
$account_perms = account_perms();

while ($perms = mysql_fetch_array($account_perms)) {
    if ($perms['privellages'] == '1')  {          
         $photo = "data/private_photos/$profile[1]/pic1.jpg";
         if (!file_exists($photo)) {
              $photo = "data/photos/0/_default.jpg";
         }
         $thumb = "data/private_photos/$profile[1]/thumb_pic1.jpg";
         if (!file_exists($thumb)) {
              $thumb = "data/photos/0/_default.jpg";
         }
         if (logged_in()) {
              echo "<li><a href=\"$photo\" rel=\"shadowbox\" title=\"<strong>$profile[2]'s Photo's</strong>\"><img src=\"$thumb\" width=\"90\" height=\"90\" alt=\"<strong>{$profile[2]}'s Photos</strong>\"  /></a></li>";

         }
     }
}

$account_perms = account_perms();
while ($perms = mysql_fetch_array($account_perms)) {
     if ($perms['privellages'] == '0')  {
        $photo = "data/private_photos/0/_default.jpg";
                  if (!file_exists($photo)) {
                       $photo = "data/photos/0/_default.jpg";
                  }
                  $thumb = "data/private_photos/0/_default.jpg";
                  if (!file_exists($thumb)) {
                       $thumb = "data/photos/0/_default.jpg";
                  }
                  if (logged_in()) {
                       echo "<li><a href=\"$photo\" rel=\"shadowbox\" title=\"<strong>$profile[2]'s Photo's</strong>\"><img src=\"$thumb\" width=\"90\" height=\"90\" alt=\"<strong>{$profile[2]}'s Photos</strong>\"  /></a></li>";

                  } 
       }
}
?>


请有人告诉我为什么我会遇到这个错误。

根据经验,当您的mysql中遇到未知错误时(顺便说一句,您不应该使用),请使用内置函数
或die(mysql\u error())
获取错误,这就是它的目的。在这种情况下,我建议您将其添加到此处


$account\u perms=mysql\u query($query,$connection)或die(mysql\u error())您将清楚地看到有关该问题的消息

首先,您的代码易受SQL注入攻击。在查询中包含变量而不转义它们。如果其中一个变量的值不正确,用户将能够覆盖其权限,甚至删除数据库中的所有内容

请阅读我在您问题下方的评论,了解如何使您的代码更安全

您遇到的问题是,$account\u perms中没有包含来自数据库的结果。在您的代码中,我只看到两个对mysql_fetch_array()的调用,因此在这种情况下,通过添加一些调试代码,应该很容易发现它不起作用:

$account_perms = account_perms();
echo 'first time using mysql_fetch';
var_dump($account_perms);
对于使用mysql_fetch_array()的另一个位置也是如此

此外,由于可能存在SQL注入漏洞,您可能会调试SQL并检查生成的SQL查询的格式是否正确。您可以将其放入您的帐户\u perms()函数中:

function account_perms() {
    global $connection;
    global $_SESSION;
    global $profile_id;
    $query = "SELECT *
        FROM ptb_permissions
        WHERE ptb_permissions.private_id = \"$profile_id\"
        AND ptb_permissions.user_id = ".$_SESSION['user_id']." ";

    $account_perms = mysql_query($query, $connection);
    confirm_query($query, $connection);
    return $account_perms;
}
// rest of code ..
var_dump($query);

$account_perms = mysql_query($query, $connection);

// etc..
[更新] 根据下面OP的评论,只有用户未登录时才会出现问题。因此,问题可能在于这一部分:

SELECT *
    FROM ptb_permissions
    WHERE ptb_permissions.private_id = \"$profile_id\"
    AND ptb_permissions.user_id = ".$_SESSION['user_id']." ";
如果用户未登录,则会话中可能未设置“user_id”键,因此生成的查询将如下所示:

SELECT *
    FROM ptb_permissions
    WHERE ptb_permissions.private_id = \"$profile_id\"
    AND ptb_permissions.user_id = 
这是无效的,导致无法从数据库返回结果

最好在代码中添加一些机制,检查用户是否登录,然后检索信息

在所有情况下,在使用变量之前,请检查您正在使用的变量是否存在并包含正确的值

例如,重新编写account_perms()函数,并将正在使用的变量作为参数传递,而不是使用全局变量

function account_perms($profile_id, $session_user_id) {
    // Convert both id's to integers (I'm assuming both are numbers here)
    // this also makes it safer
    // agains SQL injections (in this case)
    $profile_id      = (int) $profile_id;
    $session_user_id = (int) $session_user_id;

    if ($profile_id <= 0 || $session_user_id <= 0) {
        // no valid profile-id or no valid session-user-id
        // return false to indicate something was wrong
        // you may also use 'die()' with an error message
        // or Throw an Exception
        return false;
    }

    // we'll leave this global variable for now
    global $connection;

    if (!$connection) {
        die('No database connection found');
    }

    $query = "SELECT *
        FROM ptb_permissions
        WHERE ptb_permissions.private_id = " . $profile_id . "
        AND ptb_permissions.user_id = " . $session_user_id .";";

    $account_perms = mysql_query($query, $connection);
    confirm_query($query, $connection);
    return $account_perms;
}

这只是为了说明如何处理这个问题

。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果选择PDO,。查询将返回布尔值,因为存在语法错误。如果您有简单的错误处理,这对您来说是显而易见的。您需要使用单引号而不是双引号。所以:
“$profile\u id”
。不,将“$profile\u id”替换为“$profile\u id”,它仍然会出现错误OK我已经这样做了,现在它正在进行诊断,但很抱歉我不明白它说的是什么:string(119)“从ptb\u权限中选择*,其中ptb\u权限。private\u id=“1”和ptb\u权限。user\u id=1”首次使用类型为(mysql结果)字符串(119)的mysql_fetchresource(41)“从ptb_权限中选择*,其中ptb_permissions.private_id=“1”和ptb_permissions.user_id=1”首次使用类型为(mysql结果)的mysql_fetchresource(42)您可以尝试在mysql数据库中手动运行这些查询,例如,在PhpMyAdmin中运行它们并检查它们是否给出正确的结果。但是,这两次$account\u perms都包含一个类型为(mysql result)的“资源(xx)”,这意味着$account\u perms已从数据库接收到“一些”结果。这是您页面上唯一的代码,还是您也包含了其他脚本(例如,我没有看到“confirm_query()”的源代码)这个错误只有在用户注销时才会出现,但在用户登录时就会消失。如果这有助于缩小问题的范围吗?@JamesArthur是的,这确实缩小了问题的范围。如果用户未登录,变量“$\u SESSION['user\u id']”可能不会出现(我假设$\u SESSION['user\u id'])保存当前登录用户的“id”)。然后生成的查询将无效,不会返回任何结果。我会将其添加到应答器中,但决不会在公共web服务器上执行此操作!mysql_error()可能会输出不应公开的信息(查询中的错误“…where password=“mysecretpassword”)!