PHP Database Operation Helper class complete Instance _php skill

Source: Internet
Author: User

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.

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.