This example describes the PHP Database Operations helper class. Share to everyone for your reference, specific as follows:
The PHP operations database is divided into several steps (here in MySQL, for example):
1. Establish a connection
$connection =mysql_connect ($db _host, $db _username, $db _password);
2. Select Database
$db _select=mysql_select_db ($db _database);
3. Perform CRUD operations
mysql_query ("Set names ' UTF8 ')"//encoded
$result =mysql_query ($sqlstring);
(Mysql_affected_rows () the number of record rows 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 class file:
<?php
class DBHelper
{
//Establish 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 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;
}
Perform crud operation
function Excute ($sqlstring)
{
$result =mysql_query ($sqlstring);
return $result;
}
Frees 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";
? >
More about PHP Interested readers can view the site topics: "PHP based on PDO Operation Database Skills Summary", "PHP+MONGODB Database Operation Skills Encyclopedia", "PHP object-oriented Programming Program Introduction", "PHP string (String) Usage Summary", " Introduction to PHP+MYSQL Database operations and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.