Complete Helper instance for PHP database operations

Source: Internet
Author: User
This article mainly introduces the Helper class for PHP database operations, and analyzes in detail the steps for php to operate mysql connections, add, delete, modify, query, and close connections, the complete mysql operation class Helper is provided for your reference. For more information, see the following example. We will share this with you for your reference. The details are as follows:

Php database operations are divided into several steps (MYSQL is used as an example here ):

1. establish a connection

$connection=mysql_connect($db_host,$db_username,$db_password);

2. select a database

$db_select=mysql_select_db($db_database);

3. execute the CRUD operation

Mysql_query ("set names 'utf8'"); // code $ result = mysql_query ($ sqlstring );

(Mysql_affected_rows () number of records affected by the previous mysql operation)

4. query

mysql_fetch_array($result);mysql_fetch_row($result);

5. close the connection

mysql_close($connection);

DBHelper. php files:

<? Phpclass DBHelper {// establish the connection function GetConnection ($ db_host, $ db_username, $ db_password) {$ connection = mysql_connect ($ db_host, $ db_username, $ db_password ); if ($ connection = false) die ("database connection failed :". mysql_error (); // enter the specific error message return $ connection;} // select the corresponding database function DBSelect ($ db_database) {$ db_select = mysql_select_db ($ db_database ); if ($ db_select = false) die ("database selection failed :". mysql_error (); return $ db_select;} // execute C RUD operation function Excute ($ sqlstring) {$ result = mysql_query ($ sqlstring); return $ result;} // release the resource function CloseConnection ($ connection) {if ($ connection! = Null) mysql_close ($ connection) ;}}?>

Dbtext. php configuration file:

<?php$db_host="localhost";$db_database="mymessage";$db_username="root";$db_password="123456";?>

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.