note 80563 deleted from pdo.query by cmb
| From: | cmb@php.net | Date: | Sun, 05 Jan 2020 08:57:35 +0000 |
| Subject: | note 80563 deleted from pdo.query by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213791@lists.php.net to get a copy of this message | ||
Note Submitter: NUNTIUS
----
I found this method extremely useful for getting the iteration count. Note the usage of
"for" instead of "while" or "foreach". Just place the "$row =
$query->fetch()" as the second condition of your for loop (which is do until). This is the
best of both worlds IMHO. Criticism welcome.
try {
$hostname = "servername";
$dbname = "dbname";
$username = "username";
$pw = "password";
$pdo = new PDO
("mssql:host=$hostname;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
$query = $pdo->prepare("select name FROM tbl_name");
$query->execute();
for($i=0; $row = $query->fetch(); $i++){
echo $i." - ".$row['name']."<br/>";
}
unset($pdo);
unset($query);