A connection class between php and mysql
This article introduces a good php and mysql connection class, which is implemented by php5. if you need it, refer to it. Code:
Connect ();} function _ destruct () {$ this-> Close ();} private function Connect () {// database connection $ this-> link = mysql_connect ($ this-> Host, $ this-> UserName, $ this-> Password) or die ("Error Connect to DB"); $ this-> SetError (mysql_error (); // select db... mysql_select_db ($ this-> DbName); // or die ("Error Select DB"); $ this-> SetError (mysql_error ();} public function query ($ query) {// mysql query $ this-> query = mysql_query ($ query, $ This-> link); $ this-> SetError (mysql_error ();} public function assoc () {// mysql_fetch_assoc: return mysql_fetch_assoc ($ this-> query ); $ this-> SetError (mysql_error ();} public function num () {// mysql_num_rows: return mysql_num_rows ($ this-> query ); $ this-> SetError (mysql_error ();} public function result ($ index = 0) {// mysql_result: return mysql_result ($ this-> query, $ index ); $ this-> SetError (mysql_error () );} Private function SetError ($ error) {$ this-> last_error = $ error;} public function ShowError () {return $ this-> last_error;} private function Close () {mysql_close ($ this-> link) ;}}?> Call example:
query("select * from table "); //get number of result echo $con->num() . PHP_EOL; //get result echo $con->result(/* $index */) . PHP_EOL; //get all result while($row=$con->assoc()) var_dump($row); |