PHP encapsulates a database mysql class
-
- // Configure the database
- Define ('Db _ host', '127. 0.0.1 '); // server address
- Define ('Db _ user', 'root'); // USER name
- Define ('Db _ pass', ''); // password
- Define ('Db _ DATABASENAME ', 'fenxiao'); // Database
- Class Dbmysql
- {
- /*
- * Variable
- **/
- Private $ tablename = ""; // table name
- Private $ fieldname = "*";
- Private $ conn;
- Private $ where;
- Private $ SQL;
- Function _ construct ($ tablename)
- {
- // Generate a connection
- $ This-> conn = mysql_connect (DB_HOST, DB_USER, DB_PASS) or die ("connect failed". mysql_error ());
- // Select a database
- Mysql_select_db (DB_DATABASENAME, $ this-> conn );
- // Set the encoding format
- Mysql_query ("set names utf8 ");
- // Var_dump ($ conn );
- $ This-> tablename = $ tablename;
- }
- // Set SQL statements
- Private function setsql ($ SQL)
- {
- $ This-> SQL = $ SQL;
- }
- // Set the condition statement
- Public function where ($ where)
- {
- $ This-> where = "where". $ where;
- Return $ this;
- }
- // Press the specified field
- Public function field ($ keyword)
- {
- $ This-> fieldname = $ keyword;
- Return $ this;
- }
- // Set the connection query table
- Public function table ($ table1, $ table2, $ field, $ bool)
- {
- $ This-> tablename = "$ table1 left join $ table2 ON $ table1. $ field $ bool $ table2. $ field ";
- // Print_r ($ this-> tablename );
- Return $ this;
- }
-
- // Configure multi-table queries
- Public function addtable ($ table1, $ table2, $ field, $ bool)
- {
- $ This-> tablename. = "left join $ table2 ON $ table1. $ field $ bool $ table2. $ field ";
- // Print_r ($ this-> tablename );
- Return $ this;
- }
- // Set the connection query table
- # SELECT * FROM [wx_order left join wx_shopcar ON wx_shopcar.oid = wx_order.oid and wx_order.uid = left join wx_goods ON orders = users] WHERE wx_order.oid = 1 and wx_order.uid = 3
- Public function settable ($ SQL)
- {
- $ This-> tablename = $ SQL;
- // Print_r ($ this-> tablename );
- Return $ this;
- }
- // Query all databases for output in array format
- Public function select ()
- {
- /**
- * Query all data in the database
- **/
- $ Arr = array ();
- // Execute the SQL statement
- $ Result = mysql_query ("select". $ this-> fieldname. "from". $ this-> tablename. $ this-> where, $ this-> conn );
- While ($ row = mysql_fetch_assoc ($ result )){
-
- Array_push ($ arr, $ row );
- }
- Return $ arr;
- }
- // Search for specified field data
- Public function find ()
- {
- // Execute the SQL statement
- $ Result = mysql_query ("select". $ this-> fieldname. "from". $ this-> tablename. $ this-> where, $ this-> conn );
- $ Result = mysql_fetch_assoc ($ result );
- Return $ result;
- }
- // Add data to the database
- Public function add ($ data)
- {
- $ Keysql = '';
- $ Valuesql = '';
- Foreach ($ data as $ key => $ value ){
- $ Keysql. = ", '$ key "';
- $ Valuesql. = ", '$ value '";
- }
- $ Keysql = substr ($ keysql, 1 );
- $ Valuesql = substr ($ valuesql, 1 );
- $ Result = mysql_query ("insert into" '. $ this-> tablename. "' ($ keysql) VALUES ($ valuesql )");
- $ Id = mysql_insert_id ();
- // Print_r ("insert into" '. $ this-> tablename. "' ($ keysql) VALUES ($ valuesql )");
- Return $ id;
- }
- // Modify the database content
- Public function save ($ data)
- {
- $ Keysql = '';
- $ Valuesql = '';
- Foreach ($ data as $ key => $ value ){
- $ Keysql. = ", '$ key' =' $ value '";
- }
- $ Keysql = substr ($ keysql, 1 );
- // Print_r ($ keysql );
- // Echo"
";
- $ Result = mysql_query ("UPDATE" '. $ this-> tablename. "'set". $ keysql. $ this-> where );
- // Print_r ("UPDATE" '. $ this-> tablename. "'set". $ keysql. $ this-> where );
- Return $ result;
- }
- # Delete data
- Public function delete ()
- {
- $ Result = mysql_query ("delete from $ this-> tablename $ this-> where ");
- // Print_r ("delete from $ this-> tablename $ this-> where ");
- Return $ result;
- }
- }
- /**
- * Mysql_fetch_row: returns the fields in a single column [0] => "111"
- * Mysql_fetch_field: obtains the field information. [0] => ['name'] => object
- * Mysql_fetch_array returns the array data. [0] => "asasds" ['name'] =>
- */
- ?>
|
Installed, PHP, mysql