期望参数1为字符串

期望参数1为字符串

本文介绍了PDO :: query()期望参数1为字符串,给定对象警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PDO有点问题.我在2分钟前发布了另一个问题,该问题很快得到了回答,但是现在我又遇到了一个错误.

Im having a little issue with PDO.I posted another question about 2 minutes ago an that was answered pretty quickly but now I have another error.

这是代码:

                $hostname='localhost';
        $username='Elmad2';
        $password='*******';
        try {
            $dbh = new PDO("mysql:host=$hostname;dbname=portals",$username,$password);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            $stmt = $dbh->prepare('SELECT * FROM docenten');
            $stmt->setFetchMode(PDO::FETCH_ASSOC);
            $stmt->execute();
            $Result = $stmt->fetchAll();

第81行>>如果($ dbh-> query($ stmt)){

Line 81 >> if ($dbh->query($stmt)) {

                foreach ($Result as $row) {


                    echo '<div class="item"><a href="teachers/item.php?id=' . $row[id] . '">
        <h1 class="title">' . $row[title]. '</h1></a>
        <h3 class="author">Geplaatst door: ' . $row[info_bys] . '</h3><span class="description">' . $row[info_shorts] . '</span><br><img src="../' . $row[filepath] . '" class="item-image"><br>
        <a href="teachers/item.php?id='.$row[id].'"><button type="button" class="read_more btn btn-primary">Lees meer</button></a>
    </div>';



                }
            }
            else{
                echo 'error';
            }

$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

这是错误:

我希望有人能帮助我

谢谢.

推荐答案

您无需致电PDO::query().对于准备好的语句,调用PDO::prepare(),然后PDOStatement::execute()执行查询,然后可以使用PDOStatement::fetch*检索结果.

You don't need to call PDO::query(). For a prepared statement, you call PDO::prepare(), then PDOStatement::execute() performs the query and you can retrieve the result using PDOStatement::fetch*.

PDO::query()仅用于动态SQL;它以 string SQL查询语句作为参数. SELECT 语句& c.

PDO::query() is only used for dynamic SQL; it takes as its argument the string SQL query statement eg. SELECT statement, &c.

这篇关于PDO :: query()期望参数1为字符串,给定对象警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:08