PDO ne fait pas l'ancien code mysql_*
do or die()
.
est ici la syntaxe correcte:
try {
//Instantiate PDO connection
$dbh = new PDO("mysql:host=localhost;dbname=db_name", "user", "pass");
//Make PDO errors to throw exceptions, which are easier to handle
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Make PDO to not emulate prepares, which adds to security
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$query = "SELECT * FROM `some_table`";
//Prepare the statement
$stmt = $dbh->prepare($query);
//Execute it (if you had any variables, you would bind them here)
$stmt->execute();
//Work with results
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
//Do stuff with $row
}
}
catch (PDOException $e) {
//Catch any PDOExceptions that were thrown during the operation
die("An error has occurred in the database: " . $e->getMessage());
}
Vous devriez lire le PDO Manual, pour obtenir une meilleure compréhension du sujet.
Si votre requête aboutit à une ligne, peut-être que 'fetchColumn()' déplace votre pointeur vers la fin du jeu de résultats. – Quantastical
Alors, quelle est la solution? J'ai besoin de fretchColumn pour vérifier si la colonne affiche –
Commenter la ligne 'echo $ result-> fetchColumn();' – Quantastical