I am trying to get this to echo out from the database and I know there is a problem but I don't know if it is not getting the information or if I am doing a wrong way getting it to echo it out I am new to using PDO.
<?php
include 'add/dbconnect.php';
function getfevent ($conn) {
$sql = "SELECT `name` FROM `event` WHERE `featured` = 0 LIMIT 0, 30 ";
foreach ($conn->query($sql) as $row) {
echo $row['name'];
}
}
?>
Try something like this (not tested).
<?php
include 'add/dbconnect.php';
function getfevent ($conn) {
$sql = "SELECT `name` FROM `event` WHERE `featured` = 0 LIMIT 0, 30 ";
$statement=$conn->prepare($sql);
$statement->execute();
while($row=$statement->fetch()) {
echo $row['name'];
}
}
getfevent ($conn);
?>
The call to PDO::query
returns a PDOStatement
object. You do not just iterate over that object as you are trying to do. You need to utilize the fetch
, fetchAll
, fetchObject
, etc. methods on the PDOStatement
object to access or iterate through the result set.
If you do not get any error from PDO, try this:
INSERT INTO `event` (name, featured) VALUES ("name", 0)
Then see if your echo works :)