note 80563 added to function.PDO-query

From: Date: Tue, 22 Jan 2008 17:01:11 +0000
Subject: note 80563 added to function.PDO-query
Groups: php.notes 
Request: Send a blank email to php-notes+get-135312@lists.php.net to get a copy of this message
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); ---- Server IP: 208.69.120.35 Probable Submitter: 216.90.93.253 ---- Manual Page -- http://www.php.net/manual/en/function.PDO-query.php Edit -- https://master.php.net/note/edit/80563 Del: integrated -- https://master.php.net/note/delete/80563/integrated Del: useless -- https://master.php.net/note/delete/80563/useless Del: bad code -- https://master.php.net/note/delete/80563/bad+code Del: spam -- https://master.php.net/note/delete/80563/spam Del: non-english -- https://master.php.net/note/delete/80563/non-english Del: in docs -- https://master.php.net/note/delete/80563/in+docs Del: other reasons-- https://master.php.net/note/delete/80563 Reject -- https://master.php.net/note/reject/80563 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135312) next »