My Database Class _php tutorial

Source: Internet
Author: User
My Database class
Workaround




/**

* Database Configuration Class

*/



Class Dbconfig

{



public static $HOST = ' localhost ';

public static $USERNAME = ' root ';

public static $PASSWORD = ' root ';

public static $DATABASE = ' shopping ';

public static $CHARSET = ' UTF8 ';



}



?>


Copy Code



/**

* Database Operation class

*/



Class database{



Private $connection;



/**

* Construction Method

* @access Public

*/

Public Function __construct () {

$CONFIG = require (DirName (__file__). ' /dbconfig.class.php ');

$this >connection = mysql_connect (dbconfig:: $HOST, Dbconfig:: $USERNAME, Dbconfig:: $PASSWORD);

mysql_select_db (dbconfig:: $DATABASE);

mysql_query ("SET NAMES"). Dbconfig:: $CHARSET. "'");

}



/**

* Destructor method

* @access Public

*/

Public Function __destruct () {

Mysql_close ($this >connection);

}



/**

* Execute SQL query statement

* @access Private

* @param string $p _sql query command

* @return Array Recordset, no records returned empty array

*/

Private Function query ($p _sql) {

$dataTemp = mysql_query ($p _sql, $this >connection);

$data = Array ();

$dataItem = 0;

while ($rows = Mysql_fetch_assoc ($dataTemp)) {

$data [$dataItem] = $rows;

$dataItem + +;

}

return $data;

}



/**

* Execute SQL statement

* @access Public

* @param string $p _sql need to execute SQL, can be insert,select,update or delete

* @return If SQL is SELECT, returns the recordset if SQL is INSERT, returns the new record ID if SQL is update or delete, returns the number of rows affected

*/

Public Function Execute ($p _sql) {

$CONTROLR = Strtoupper (substr ($p _sql,0,6));

Switch ($CONTROLR) {

Case ' INSERT ':

mysql_query ($p _sql, $this >connection);

$result = mysql_insert_id ($this >connection);

Break

Case ' SELECT ':

$result = $this >query ($p _sql, $this >connection);

Break

Default

mysql_query ($p _sql, $this >connection);

$result = Mysql_affected_rows ($this >connection);

Break

}

return $result;

}



}



?>


Copying code calls is simple:
$sql = ' ... '; can be any additions and deletions to change the statement
$db = new DataBase ();
$rs = $db >execute ($sql);
$DB = null;

Please give us a lot of advice!

[ ]


d8888d Huitie Content
Look at it ~ ~ [img]http://www.phpchina.com/bbs/images/smilies/default/shy.gif[/img][img]http://www.phpchina.com/bbs/ IMAGES/SMILIES/DEFAULT/SHY.GIF[/IMG]

http://www.bkjia.com/PHPjc/632535.html www.bkjia.com true http://www.bkjia.com/PHPjc/632535.html techarticle my Database class solution/** * DB Configuration class */class Dbconfig {public static $HOST = ' localhost ', public static $USERNAME = ' root ' ; public static $PASSWORD = ' root ';

  • 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.