Database Connection operations
Database Connection operations
-
- Class mysql
- {
- Private $ _ link;
- Public function _ construct ($ dbhost, $ dbuser, $ dbpassword, $ dbname, $ charset = 'utf8 ')
- {
- $ This-> _ link = mysql_connect ($ dbhost, $ dbuser, $ dbpassword, true);/* connect to database */
- $ This-> _ link or $ this-> errmsg ('Can \'t connect to MySQL server! ');/* Whether the connection is successful */
- If ($ this-> version ()> '4. 1') {/* check the database version */
- $ This-> query ('set names'. $ charset);/* set Database encoding */
- }
- Mysql_select_db ($ dbname, $ this-> _ link) or $ this-> errmsg ('Can \'t select the database! ');/* Open the database */
- }
- /* Perform database operations */
- Public function query ($ SQL)
- {
- $ Result = mysql_query ($ SQL, $ this-> _ link );
- $ Result or $ this-> errmsg ('execute SQL sentence error! ');
- Return $ result;
- }
- /* Return the array generated based on the row obtained from the result set */
- /* MYSQL_BOTH get an array containing both join and numeric indexes (like mysql_fetch_array ())*/
- /* MYSQL_ASSOC: get an array containing both join and digital indexes (like mysql_fetch_assoc ())*/
- /* MYSQL_NUM get an array containing both join and numeric indexes (like mysql_fetch_row ())*/
- Public function fetch_array ($ result, $ type = MYSQL_BOTH)
- {
- Return mysql_fetch_array ($ result, $ type );
- }
-
- /* Return the object generated based on the obtained row */
- Public function fetch_object ($ result)
- {
- Return mysql_fetch_object ($ result );
- }
- /* Obtain the number of rows affected by the previous MySQL operation */
- Public function affected_rows ()
- {
- Return mysql_affected_rows ($ this-> _ link );
- }
- /* Release the result memory */
- Public function free_result ($ result)
- {
- Return mysql_free_result ($ result );
- }
- /* Number of rows in the result set */
- Public function num_rows ($ result)
- {
- Return mysql_num_rows ($ result );
- }
- /* Number of fields in the result set */
- Public function num_fields ($ result)
- {
- Return mysql_num_fields ($ result );
- }
- /* Obtain the ID generated by the previous INSERT operation */
- Public function insert_id ()
- {
- Return mysql_insert_id ($ this-> _ link );
- }
- /* Issue a mysql execution error */
- Private function errmsg ($ msg)
- {
- $ Message ='A mysql error has occurred!
';
- $ Message. ='Error Number:'. Mysql_errno ($ this-> _ link ).'
';
- $ Message. ='Error Description:'. $ Msg. mysql_error ($ this-> _ link ).'
';
- $ Message. ='Error Time:'. Date ('Y-m-d H: I: s ');
- Exit ($ message );
- }
- /* Return the connection id */
- Public function link_id ()
- {
- Return $ this-> _ link;
- }
- /* Return the database server version */
- Public function version (){
- Return mysql_get_server_info ($ this-> _ link );
- }
- /* Obtain the real IP address of the client */
- Function getip (){
- If (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown ")){
- $ Ip = getenv ("HTTP_CLIENT_IP ");
- } Else
- If (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown ")){
- $ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
- } Else
- If (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown ")){
- $ Ip = getenv ("REMOTE_ADDR ");
- } Else
- If (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown ")){
- $ Ip = $ _ SERVER ['remote _ ADDR '];
- } Else {
- $ Ip = "unknown ";
- }
- Return ($ ip );
- }
- }
|