PHP数据库操作Helper类完整实例,phphelper_PHP教程

WBOY
Release: 2016-07-12 08:52:47
Original
1603 people have browsed it

PHP数据库操作Helper类完整实例,phphelper

本文实例讲述了PHP数据库操作Helper类。分享给大家供大家参考,具体如下:

php操作数据库分为几个步骤(这里以MYSQL为例):

1. 建立连接

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

Copy after login

2. 选择数据库

$db_select=mysql_select_db($db_database);

Copy after login

3. 执行CRUD操作

mysql_query("set names 'utf8'");//编码
$result=mysql_query($sqlstring);

Copy after login

(mysql_affected_rows()前一次mysql操作所影响的记录行数)

4. 查询

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

Copy after login

5. 关闭连接

mysql_close($connection);

Copy after login

DBHelper.php类文件:

<&#63;php
class DBHelper
{
  //建立连接
  function GetConnection($db_host,$db_username,$db_password)
  {
    $connection=mysql_connect($db_host,$db_username,$db_password);
    if($connection==false)
      die("数据库连接失败:".mysql_error());//输入具体错误信息
    return $connection;
  }
  //选择对应数据库
  function DBSelect($db_database)
  {
    $db_select=mysql_select_db($db_database);
    if($db_select==false)
      die("数据库选择失败:".mysql_error());
    return $db_select;
  }
  //执行CRUD操作
  function Excute($sqlstring)
  {
    $result=mysql_query($sqlstring);
    return $result;
  }
  //释放资源
  function CloseConnection($connection)
  {
    if($connection!=null)
    mysql_close($connection);
  }
}
&#63;>

Copy after login

dbtext.php配置文件:

<&#63;php
$db_host="localhost";
$db_database="mymessage";
$db_username="root";
$db_password="123456";
&#63;>

Copy after login

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125874.htmlTechArticlePHP数据库操作Helper类完整实例,phphelper 本文实例讲述了PHP数据库操作Helper类。分享给大家供大家参考,具体如下: php操作数据库分为几个...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!