PHP PDO:调用成员函数prepare() &列表?

发布于 2024-10-05 22:27:26 字数 1158 浏览 7 评论 0原文

致命错误:在非对象上调用成员函数prepare()

当我尝试执行以下操作时,我得到的结果是:

$sql = $connect->prepare ("SELECT id, pwd, firstname, lastname, approved, user_level FROM users WHERE $user_cond AND banned = 0"); // SELECT
$sql->execute ();
$sql->setFetchMode(PDO::FETCH_ASSOC);
$num = $connect->rowCount();

我坚持重写我的系统,取出所有 mysql_* 并改用 pdo。

这就是以前的情况:

$result = mysql_query("SELECT `id`,`pwd`,`firstname`,`lastname`,`approved`,`user_level` FROM users WHERE 
           $user_cond
            AND `banned` = '0'
            ") or die (mysql_error()); 
$num = mysql_num_rows($result);

我做错了什么?

对于 list() 我有:

list($id,$pwd,$firstname,$lastname,$approved,$user_level) = mysql_fetch_row($result);

而不是 mysql_fetch_row($result);我应该在 PDO 中做什么?

我的 PDO 对象/连接:

try{
$connect = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset:UTF-8", DB_USER, DB_PASS, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(PDOException $pe)
{
  die('Could connect to the database because: ' .$pe->getMessage());

}

Fatal error: Call to a member function prepare() on a non-object

Is what I am getting when I try to do:

$sql = $connect->prepare ("SELECT id, pwd, firstname, lastname, approved, user_level FROM users WHERE $user_cond AND banned = 0"); // SELECT
$sql->execute ();
$sql->setFetchMode(PDO::FETCH_ASSOC);
$num = $connect->rowCount();

Im holding on rewriting my system, taking out all mysql_* and make use of pdo instead.

This is what was before:

$result = mysql_query("SELECT `id`,`pwd`,`firstname`,`lastname`,`approved`,`user_level` FROM users WHERE 
           $user_cond
            AND `banned` = '0'
            ") or die (mysql_error()); 
$num = mysql_num_rows($result);

What have I done wrong?

And with list() I have:

list($id,$pwd,$firstname,$lastname,$approved,$user_level) = mysql_fetch_row($result);

instead of mysql_fetch_row($result); what should I do in PDO?

My PDO object/connection:

try{
$connect = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset:UTF-8", DB_USER, DB_PASS, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(PDOException $pe)
{
  die('Could connect to the database because: ' .$pe->getMessage());

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

皇甫轩 2024-10-12 22:27:26

致命错误:调用成员函数
在非对象上准备()

通常意味着某些事情失败了并且你的变量不是你期望的那样。 $connection 可能是false

PDOStatement::fetch

list($id,$pwd,$firstname,$lastname,$approved,$user_level) = $sql->fetch();

同时调用 rowCount声明,而不是连接:

$sql->rowCount();

Fatal error: Call to a member function
prepare() on a non-object

usually means that something failed and your variable is not what you expect it to be. $connection is probably false.

PDOStatement::fetch

list($id,$pwd,$firstname,$lastname,$approved,$user_level) = $sql->fetch();

Also call rowCount on the statement, not on the connection:

$sql->rowCount();
尸血腥色 2024-10-12 22:27:26

由于您使用准备,您应该将 $cond 设为命名参数,然后执行以下操作:
在此示例中,条件只是 user_id,但您可以有多个条件和多个命名参数,只需以相同的方式对每个条件进行 bind()

$sth = $pdo->prepare ("SELECT id, pwd, firstname, lastname, approved, user_level FROM users WHERE user_id = :cond AND banned = 0"); 
$sth->bindParam(':cond', $some_user_id);
$sth->execute ();
$a = $sth->fetch( PDO::FETCH_ASSOC );

$a 将是您的关联数组, print_r($a) 查看其中的内容

Since you using prepare, you should make the $cond a named parameter and then do this:
in this example the condition is just user_id, but you can have multiple conditions and multiple named parameters, just bind() each one the same way

$sth = $pdo->prepare ("SELECT id, pwd, firstname, lastname, approved, user_level FROM users WHERE user_id = :cond AND banned = 0"); 
$sth->bindParam(':cond', $some_user_id);
$sth->execute ();
$a = $sth->fetch( PDO::FETCH_ASSOC );

The $a will be your assiciate array, print_r($a) to see what's in it

时光礼记 2024-10-12 22:27:26

看起来像是范围问题。

您是在函数、类、命名空间中分配 $connect 还是在函数、类或命名空间中运行准备?

如果您在函数中分配 $connect ,那么该函数应该返回 PDO 连接。我会像这样引入一个静态实例:

class Database extends PDO
{
    private $instance = null;
    public static function Instance()
    {
        if(self::$instance == null)
        {
            self::$instance = new Database();
        }
        return self::$instance;
    }

    private function __construct(){}
    private function __connect()
    {
        try 
        {
            parent::__construct("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset:UTF-8", DB_USER, DB_PASS, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION))
        }catch(PDOException $e)
        {
            die($e->getMessage());
        }
    }
}

然后像这样使用:

$Database = Database::Instnace();

$Database->prepare("...");

Looks like a scope issue.

Are you assigning $connect within a function, class, namespace or are you running prepare in a function, class or namespace ?

if your assigning $connect within a function then that function should return the PDO Connection. I would introduce a static instance like so:

class Database extends PDO
{
    private $instance = null;
    public static function Instance()
    {
        if(self::$instance == null)
        {
            self::$instance = new Database();
        }
        return self::$instance;
    }

    private function __construct(){}
    private function __connect()
    {
        try 
        {
            parent::__construct("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset:UTF-8", DB_USER, DB_PASS, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION))
        }catch(PDOException $e)
        {
            die($e->getMessage());
        }
    }
}

then use like so:

$Database = Database::Instnace();

$Database->prepare("...");
太阳公公是暖光 2024-10-12 22:27:26

该错误很可能是由于 MySQL 连接失败 - 因此 $connect 不是有效的 PDO 对象 - 因此,正如错误所述,您正在尝试调用一个对象上的方法,但该对象不支持它。您的连接处理代码是什么样的?您检查连接是否已成功创建?

The error is most likely due to the MySQL connection failing - therefor $connect is not a valid PDO object - so as the error says, you're trying to call a method on an object which doesn't support it. What does your connection handling code look like? Did you check if a connection was successfully created?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文