PHP database and Operation instances are not very powerful, but they are quite convenient to use.
PHP database and Operation instances are not very powerful, but they are quite convenient to use.
Db. class. php
PHP code
-
- /**
- * Database
- */
- Class DataBase
- {
- Var $ pConnect = FALSE; // whether to use persistent connections
- Var $ mHost; // database host
- Var $ mDatabase;
- Var $ db; // Database
- Var $ mUser; // database username
- Var $ mPwd; // database user password
- Var $ mConn; // Connection ID
- Var $ result; // result resource ID for executing the query command
- Var $ num_rows; // number of returned entries
- Var $ insert_id; // returns the ID of the last INSERT command.
- Var $ affected_rows; // number of columns affected by the query command returned
- // The number of columns (row) affected by INSERT, UPDATE, or DELETE.
- // If delete does not contain the where clause, 0 is returned.
- // Constructor
- Public function _ construct ($ host, $ user, $ pwd, $ db)
- {
- $ This-> mHost = $ host;
- $ This-> mUser = $ user;
- $ This-> mPwd = $ pwd;
- $ This-> db = $ db;
- }
- // Database connection
- Public function connect ()
- {
- If ($ this-> pConnect)
- $ This-> mConn = mysql_pconnect ($ this-> mHost, $ this-> mUser, $ this-> mPwd); // persistent connection
- Else
- $ This-> mConn = mysql_connect ($ this-> mHost, $ this-> mUser, $ this-> mPwd); // short connect
- If (! $ This-> mConn) $ this-> dbhalt ("cannot connect to the database! ");
- If ($ this-> db = "") $ this-> db = $ this-> dbDatabase;
- If (! Mysql_select_db ($ this-> db, $ this-> mConn ))
- $ This-> dbhalt ("The database is unavailable! ");
- } // Eof # dbconnect ()
- // Change the database
- Public function dbChange ($ db ){
- $ This-> db = $ db;
- $ This-> connect ();
- }
- // Execute the SQL statement and return the result resource id
- Public function execute ($ SQL ){
- $ This-> result = mysql_query ($ SQL );
- Return $ this-> result;
- }
- // Retrieve array-index and association
- Public function fetchArray ($ resultType = MYSQL_BOTH)
- {
- Return mysql_fetch_array ($ this-> result, $ resultType );
- }
- // Obtain the associated array
- Public function fetchAssoc ()
- {
- Return mysql_fetch_assoc ($ this-> result );
- }
- // Obtain the number index array
- Public function fetchIndexArray ()
- {
- Return mysql_fetch_row ($ this-> result );
- }
- // Obtain the object array
- Public function fetchObject ()
- {
- Return mysql_fetch_object ($ this-> result );
- }
- // Number of returned Records
- Function numRows ()
- {
- Return mysql_num_rows ($ this-> result );
- }
- // Return all database names in the host
- Public function dbNames ()
- {
- $ RsPtr = mysql_list_dbs ($ this-> mConn );
- $ I = 0;
- $ Cnt = mysql_num_rows ($ rsPtr );
- While ($ I <$ cnt)
- {
- $ Rs [] = mysql_db_name ($ rsPtr, $ I );
- $ I ++;
- }
- Return $ rs;
- }
- Function dbhalt ($ errmsg ){
- $ Msg = "database error! ";
- $ Msg = $ errmsg;
- Echo "$ msg ";
- Die ();
- }
- // Delete
- Function delete ($ SQL ){
- $ Result = $ this-> execute ($ SQL, $ dbbase );
- $ This-> affected_rows = mysql_affected_rows ($ this-> dbLink );
- $ This-> free_result ($ result );
- Return $ this-> affected_rows;
- }
- // Add
- Function insert ($ SQL ){
- $ Result = $ this-> execute ($ SQL, $ dbbase );
- $ This-> insert_id = mysql_insert_id ($ this-> dbLink );
- $ This-> free_result ($ result );
- Return $ this-> insert_id;
- }
- // Modify
- Function update ($ SQL ){
- $ Result = $ this-> execute ($ SQL, $ dbbase );
- $ This-> affected_rows = mysql_affected_rows ($ this-> dbLink );
- $ This-> free_result ($ result );
- Return $ this-> affected_rows;
- }
- // Close the connection
- Function dbclose (){
- Mysql_close ($ this-> dbLink );
- }
- } // End class
- ?>
Directly reference:
PHP code
-
- Include "db. class. php ";
- $ Mydb = new DataBase ("localhost", "root", "123456", "test ");
- $ Mydb-> connect ();
- $ Mydb-> execute ("set names GBK ");
- $ Mydb-> execute ("select * from usrs ");
- Print_r ($ mydb-> dbNames ());
- ?>