Package for MySQL operation Class (PHP)

Source: Internet
Author: User

<?PHPclassmysql{/** * Error function * * @param string $error*/function Err ($error) {die ("Sorry, your operation is wrong, the cause of the error is:". $error);//Die has two functions, output and termination are equivalent to echo and exit combinations    }    /** * Connect to Database * * @param string $dbhost hostname * @param string $dbuser username * @param string $dbpsw password * @param string $dbname database name * @param string $dbcharset Character Set/encoding * @return BOOL Connection succeeded or unsuccessful **/function Connect ($config) {extract ($config); if(! ($con = mysql_connect ($dbhost, $dbuser, $DBPSW))) {//mysql_connect Connecting database functions$ This-Err (Mysql_error ()); }        if(!mysql_select_db ($dbname, $con)) {//mysql_select_db functions for selecting libraries$ This-Err (Mysql_error ()); } mysql_query ("Set Names". $dbcharset);//set encoding format using mysql_query: mysql_query ("Set names UTF8")    }    /** * Execute SQL statement * * @param string $sql * @return bool Returns execution success, resource, or execution failure*/function Query ($sql) {if(! ($query = mysql_query ($sql))) {//Execute SQL statements using the Mysql_query function$ This->err ($sql."<br/>". Mysql_error ());//mysql_error Error}Else{            return$query; }    }    /** * list * * @param source $query SQL statement Resources executed through mysql_query * @return Array returns a list of arrays **/function FindAll ($query) { while($rs =mysql_fetch_array ($query, MYSQL_ASSOC)) {//The mysql_fetch_array function transforms the resource into an array, one at a time.$list []=$rs; }        returnIsset ($list)? $list:""; }    /** * Single * * * @param source $query SQL statement by mysql_query execution of resources *return array returns a single array of information **/function FindOne ($query) {$rs=mysql_fetch_array ($query, MYSQL_ASSOC); return$rs; }    /** * Specifies the value of the specified field of the row * * @param source $query SQL statement by mysql_query execution of the resource *return array returns the value of the specified field for the specified row **/function Findresult ($query, $row=0, $filed =0) {$rs=mysql_result ($query, $row, $filed); return$rs; }    /** * Add Function * * @param string $table table name * @param array $arr add arrays (one-dimensional array containing fields and values) **/function Insert ($table, $arr) {//$sql = "INSERT into table name (multiple fields) values (multiple values)"; //mysql_query ($sql);        foreach($arr as$key = $value) {//foreach Loop array$value = mysql_real_escape_string ($value);//To escape special characters in a string used in an SQL statement$KEYARR [] ="`". $key."`";//Save the key names in the $arr array to the $keyarr array$VALUEARR [] ="'". $value."'";//Save the key values in the $arr array to $valuearr, because the value is more of a string, and the SQL statement inside the insert if the value is a string words to add single quotation marks, so this place to add single quotation marks} $keys= Implode (",", $KEYARR);//The implode function is to put the array group into a string implode (delimiter, array)$values = Implode (",", $VALUEARR); $sql="Insert into". $table."(". $keys.") VALUES (". $values.")";//SQL INSERT statement format: INSERT into table (multiple fields) values (multiple values)$ This->query ($sql);//invoke the query (execute) method of the class itself to execute this SQL statement note: $this refer to itself        returnMYSQL_INSERT_ID ();//returns the ID generated by the INSERT operation in the previous step    }    /** * Modify function * * @param string $table table name * @param array $arr Modify Array (one-dimensional array containing fields and values) * @param string $where condition * */function Update ($table, $arr, $where){        //Update table name set field = field value where ...        foreach($arr as$key =$value) {$value=mysql_real_escape_string ($value); $KEYANDVALUEARR []="`". $key."' = '". $value."'"; } $keyAndvalues= Implode (",", $KEYANDVALUEARR); $sql="Update". $table."Set". $keyAndvalues."where".$where;//Modify Action Format Update table name set field = value Where Condition$ This-query ($sql); }    /** * Delete function * * @param string $table table name * @param string $where condition **/function del ($table, $where) {$sql="Delete from". $table."where".$where;//Delete SQL statement format: Delete from table name where condition$ This-query ($sql); }}?>

Package for MySQL operation Class (PHP)

Related Article

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.