This article mainly introduces the php-encapsulated connection Mysql class and usage. based on php-encapsulated simple MySQL database connection, query, traversal, and other techniques, it also describes the specific usage, for more information about how to connect to Mysql in php, see the following example. We will share this with you for your reference. The details are as follows:
Class mysql {private $ db_name; private $ db_host; private $ db_user; private $ db_pwd; private $ conn; private $ querysql; private $ result; private $ resultarray = array (); private $ row; // Create a constructor database name host name username password function _ counstruct ($ dbname, $ dbhost, $ dbuser, $ dbpwd) {$ this-> db_name = $ dbname; $ this-> db_host = $ dbhost; $ this-> db_pwd = $ dbpwd; $ this-> db_user = $ dbuser; $ this-> dbconnect (); $ this-> selectdb ();} // connect to the database private function db_connect () {$ this-> conn = mysql_connect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd) or die ("cocould not Connect MySql Server ");} private function selectdb () {mysql_select_db ($ this-> db_name) or die ("unable to select dbname")} private function query () {return $ this-> result = mysql_query ($ this-> querysql);} private function get_result ($ SQL) {$ this-> querysql = $ SQL; $ this-> query (); if ($ this-> get_num ()> 0) {// mysql_fetch_assoc () and mysql_fetch_array (, MYSQL_ASSOC) returns falsewhile ($ this-> rows = mysql_fetch_array ($ this-> result) if a row is obtained from the result set as an associated array )) {// assign an array value to resultarray [] = assign the number of affected rows to the array $ this-> resultarray [] = $ this-> rows} return $ this-> resultarray ;}} // $ result returned value is of the bool type. false indicates no data. private function get_num () {return $ this-> num = mysql_num_rows ($ this-> result );}} $ m = new mysql ("testuser", "localhost", "root", "root"); $ arreresult = $ m-> get_result ("select * from userinfo ");
I hope this article will help you with php programming.