本文分享一个好用的php与mysql操作类,此mysql类与其它类的不同在于,可以设置表的读、写锁。有需要的朋友参考下吧。分享一个php与mysql操作类,代码:
getconnected()) { $this->closeconnection(); } if($this->connection = ($bpersistant ? mysql_pconnect($shost, $suser, $spassword) : mysql_connect($shost, $suser, $spassword))) { $this->setconnected(true); if($sdatabase) { $this->setdb($sdatabase); } return true; } else { $this->setconnected(false); return false; } } /** * this is the destructor of this class. it frees the result of a query, * it unlocks all locked tables and close the connection to the database * it does not return anything at all, so you will not know if it was sauccessfull * * @access public */ function _mysql() { if($this->result) { $this->freeresult(); } if($this->getlocked()) { $this->unlock(); } if($this->getconnected()) { $this->closeconnection(); } } /** * this function frees the result from a query if there is any result. * * @access public */ function freeresult() { if($this->result) { @mysql_free_result($this->result); } } /** * this function executes a query to the database. * the function does not return the result of the query, you must call the * function getqueryresult() to fetch the result * * @param string the query-string to execute * @return boolean true if the query was successfull * @access public */ function query($query) { if(strlen(trim($query)) == 0) { $this->printerror(no query got in function query()); return false; } if(!$this->getconnected()) { $this->printerror(not connected in function query()); return false; } $querytype = substr(trim($query), 0, strpos($query, )); $this->setquerytype($querytype); $this->result = mysql_query($query, $this->connection); if($this->result) { return true; } return false; } /** * sets the querytype of the last query executed * for example it can be select, update, delete etc. * * @access private */ function setquerytype($type) { $this->querytype = strtoupper($type); } /** * returns the querytype * * @return string * @access private */ function getquerytype() { return $this->querytype; } /** * this function returns number of rows got when executing a query * * @return mixed false if there is no query-result. * if the querytype is select then it will use the function mysql_num_rows * otherwise it uses the mysql_affected_rows * @access public */ function getnumrows() { if($this->result) { if(debug==true) { print(.$this->getquerytype().
); } return mysql_affected_rows($this->connection); } return false; } /** * the function returns the result from a call to the query() function * * @return object * @access public */ function getqueryresult() { return $this->result; } /** * this function returns the query result as an array for each row in the query result * * @return array * @access public */ function fetcharray() { if($this->result) { return mysql_fetch_array($this->result); } return false; } /** * this function returns the query result as an object for each row in the query result * * @return object * @access public */ function fetchobject() { if($this->result) { return mysql_fetch_object($this->result); } return false; } /** * this function returns the query result as an array for each row in the query result * * @return array * @access public */ function fetchrow() { if($this->result) { return mysql_fetch_row($this->result); } return false; } /** * this function sets the database * * @return boolean true if the database was set * @access public */ function setdb($sdatabase) { if(!$this->getconnected()) { $this->printerror(not connected in function setdb()); return false; } if($this->selecteddb = mysql_select_db($sdatabase, $this->connection)) { return true; } return false; } /** * this function returns a flag so you can see if you are connected to the database * or not * * @return boolean true when connected to the database * @access public */ function getconnected() { return $this->isconnected; } /** * this function sets the flag so you can see if you are connected to the database * * @param $bstatus the status of the connection. true if you are connected, * false if you are not * @access public */ function setconnected($bstatus) { $this->isconnected = $bstatus; } /** * the function unlocks tables if there are locked tables and the closes the * connection to the database. * * @access public */ function closeconnection() { if($this->getlocked()) { $this->unlock(); } if($this->getconnected()) { mysql_close($this->connection); $this->setconnected(false); } } /** * unlocks all tables that are locked * * @access public */ function unlock() { if(!$this->getconnected()) { $this->setlocked(false); } if($this->getlocked()) { $this->query(unlock tables); $this->setlocked(false); } } /** * this function locks the table(s) that you specify * the type of lock must be specified at the end of the string. * * @param string a string containing the table(s) to lock, * as well as the type of lock to use (read or write) * at the end of the string * @return boolean true if the tables was successfully locked * @access private */ function lock($scommand) { if($this->query(lock table .$scommand)) { $this->setlocked(true); return true; } $this->setlocked(false); return false; } /** * this functions sets read lock to specified table(s) * * @param string a string containing the table(s) to read-lock * @return boolean true on success */ function setreadlock($stable) { return $this->lock($stable. .locked_for_read); } /** * this functions sets write lock to specified table(s) * * @param string a string containing the table(s) to read-lock * @return boolean true on success */ function setwritelock($stable) { return $this->lock($stable. .locked_for_write); } /** * sets the flag that indicates if there is any tables locked * * @param boolean the flag that will indicate the lock. true if locked */ function setlocked($bstatus) { $this->islocked = $bstatus; } /** * returns true if there is any locked tables * * @return boolean true if there are locked tables */ function getlocked() { return $this->islocked; } /** * prints an error to the screen. can be used to kill the application * * @param string the text to display * @param boolean true if you want to kill the application. default is false */ function printerror($text, $killapp=false) { if($text) { print(error
.$text); } if($killapp) { exit(); } } /** * display any mysql-error * * @return mixed string with the error if there is any error. * otherwise it returns false */ function getmysqlerror() { if(mysql_error()) { return
mysql error number .mysql_errno().
.mysql_error(); } return false; }}?>