A connection class between php and mysql

Source: Internet
Author: User
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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.