note 87110 added to pdostatement.rowcount

From: Date: Wed, 19 Nov 2008 19:32:36 +0000
Subject: note 87110 added to pdostatement.rowcount
Groups: php.notes 
Request: Send a blank email to php-notes+get-146725@lists.php.net to get a copy of this message
My rowCount() workaround & how it's used: <?php class MyPDO extends PDO { private $queryString; public function query(/* ... */) { $args = func_get_args(); $this->queryString = func_get_arg(0); return call_user_func_array(array(&$this, 'parent::query'), $args); } public function rowCount() { $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i'; if (preg_match($regex, $this->queryString, $output) > 0) { $stmt = parent::query("SELECT COUNT(*) FROM {$output[1]}", PDO::FETCH_NUM); return $stmt->fetchColumn(); } return false; } } $pdo = new MyPDO("sqlite::memory:"); $result = $pdo->query("SELECT row1, row2 FROM table WHERE something = 5"); if ($pdo->rowCount() > 0) { echo "{$result['row1']}, {$result['row2']}"; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 99.241.132.158 ---- Manual Page -- http://www.php.net/manual/en/pdostatement.rowcount.php Edit -- https://master.php.net/note/edit/87110 Del: integrated -- https://master.php.net/note/delete/87110/integrated Del: useless -- https://master.php.net/note/delete/87110/useless Del: bad code -- https://master.php.net/note/delete/87110/bad+code Del: spam -- https://master.php.net/note/delete/87110/spam Del: non-english -- https://master.php.net/note/delete/87110/non-english Del: in docs -- https://master.php.net/note/delete/87110/in+docs Del: other reasons-- https://master.php.net/note/delete/87110 Reject -- https://master.php.net/note/reject/87110 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#146725) next »