Reputation: 50
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.
Here is the code:
$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();
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();
}
And here is the error:
Warning: PDO::query() expects parameter 1 to be string, object given in >/home/ubuntu/workspace/portals/teachers.php on line 81
I hope someone can help me,
Thanks.
Upvotes: 1
Views: 6367
Reputation: 50
@rizier123 answered my question.
I just needed to delete the if statement and add quotes to the row data.
Thanks @rizier123
Upvotes: 0
Reputation: 5246
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()
is only used for dynamic SQL; it takes as its argument the string SQL query statement eg. SELECT
statement, &c.
Upvotes: 1