I'm probably being stupid and missing an obvious error in my code. However, I can't spot it so would appreciate any help available.
This code works fine:
Connection to the database is confirmed and the row count is correctly reported. However when I add the following snippet no errors are reported but no output is shown either:
I'd appreciate any help anyone can offer on this please?
This code works fine:
Code:
<?php
include-once ('config.php');
$dsn = "mysql:host=$host;dbname=$dbname;charset=$charset;port=$port";
try {
// create a PDO connection with the configuration data
$conn = new PDO($dsn, $user, $dbpassword);
// display a message if connected to database successfully
if($conn) {
echo "Connected to the " . $dbname . " database successfully!<br>";
}
} catch (PDOException $e) {
// report error message
echo $e->getMessage();
}
$sql = "SELECT COUNT(*) AS num FROM checks";
$stmt = $conn->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo '<br>' . $row['num'] . ' users exist.';
?>
Connection to the database is confirmed and the row count is correctly reported. However when I add the following snippet no errors are reported but no output is shown either:
Code:
$id = '108'; // for testing - live $id comes from login form
$sql = "SELECT * FROM checks WHERE ID = ?";
$stmt = $conn->prepare($sql);
$stmt->execute($id);
$users = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->errorCode() == 0) {
while(($users = $stmt->fetch()) != false) {
echo $users['checks'] . "\n";
}
} else {
$errors = $stmt->errorInfo();
echo($errors[2]);
}
I'd appreciate any help anyone can offer on this please?