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";?>