I am trying to fetch results from a database, it returns no errors and it returns no results. Any ideas as to what I am doing wrong?
try {
$db_connection = new PDO("mysql:host = $hostname; dbname = $database", $username, $password);
//$db_connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
$query = $db_connection->prepare("SELECT `id`, `code` FROM `mycodedatabase` WHERE 1");
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo $row['id'];
echo $row['code'];
}
$db_connection = null;
}
catch(PDOException $e) {
echo $e->getMessage();
}
Try to avoid using the same variable for different purposes. Use $query and $result. Don’t make your code complex. I think it will help while debugging and avoid errors when logic seems correct.
Don’t forget to change "user" and "pass"