note 101991 added to ref.mysql

From: Date: Fri, 21 Jan 2011 20:55:08 +0000
Subject: note 101991 added to ref.mysql
Groups: php.notes 
Request: Send a blank email to php-notes+get-175980@lists.php.net to get a copy of this message
I just put together this class as a simple extensible wrapper for my database queries, and thought it might come in handy for someone else. The Db class at the top shows how it can be extended. The queryAsArray is just a convenience function for query(); both functions are quite useful. <?php class Db extends DbBase{ protected $addr = "localhost:3306"; protected $user = "myusername"; protected $db = "mydatabase"; protected $pwd = "mypassword"; } class DbBase { private $con; protected $addr; protected $user; protected $db; protected $pwd; function __construct(){ $this->con = mysql_connect($this->addr, $this->user, $this->pwd); if (!$this->con){ die('Could not connect: ' . mysql_error()); } mysql_select_db($this->db); } function __destruct(){ mysql_close($this->con); } /* Execute a myqsl query. Optionally set the number of records affected. Connection is handled automatically by the constructor and destructor. */ function query($sql, &$records = null){ $sql = str_replace(array('\\"', "\\'"), array('"', "'"), $sql); $result = mysql_query($sql);// or die(mysql_error()); if (!$result){ $err = mysql_error(); die($err); } if (isset($records)){ $records = mysql_affected_rows($this->con); } return $result; } /* turns your result into an array (valid typs are MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH) */ function queryAsArray($sql, $type = MYSQL_ASSOC){ $result = $this->query($sql); if(!is_resource($result)){ trigger_error("query did not return a valid resource", E_USER_WARNING); return $result; } $data = array(); while($row = mysql_fetch_array($result, $type)) $data[] = $row; return $data; } }//end class //example usage $db = new Db(); $sql = "SHOW TABLES;"; $res = $db->queryAsArray($sql, MYSQL_NUM); var_dump($res); //example using query() $table = $res[0][0]; $sql = "SELECT * FROM $table"; $rows = -1; $res = $db->query($sql, $rows); echo "Found $rows entries in table $table\n"; while ($row = mysql_fetch_array($res)){ //do some processing.. var_dump ($row); } //example using queryAsArray() $res = $db->queryAsArray($sql); echo "Found " . count($res) . " entries in table $table\n"; var_dump($res); //queries that modify are just as easy. passing $rows is optional, but if you use it, declare it first. $sql = "DELETE FROM $table WHERE true = false;"; if ($db->query($sql, $rows)) echo "$rows records affected.\n"; else echo "Error in query: '$sql'.\n"; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 76.8.208.18 ---- Manual Page -- http://www.php.net/manual/en/ref.mysql.php Edit -- https://master.php.net/note/edit/101991 Del: integrated -- https://master.php.net/note/delete/101991/integrated Del: useless -- https://master.php.net/note/delete/101991/useless Del: bad code -- https://master.php.net/note/delete/101991/bad+code Del: spam -- https://master.php.net/note/delete/101991/spam Del: non-english -- https://master.php.net/note/delete/101991/non-english Del: in docs -- https://master.php.net/note/delete/101991/in+docs Del: other reasons-- https://master.php.net/note/delete/101991 Reject -- https://master.php.net/note/reject/101991 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#175980) next »