php DBhelp

最近学习php,学了几天,写了一个DBhelper类,贴出代码,(不足之处可以拍砖)

<?php
   class DBHelper{
    private $mysqli;
    private static $host=‘127.0.0.1‘;
    private static $user=‘root‘;
    private static $pwd=‘‘;
    private static $dbname=‘‘;

    //通过构造方法进行初始化操作
    public function __construct(){
        $this->mysqli=new mysqli(self::$host,self::$user,self::$pwd,self::$dbname)
        or die(‘数据库链接出错:‘.$this->mysqli->connect_error);
        //设置数据库编码为utf8
        $this->mysqli->query(‘set names utf8‘);
    }    

    //执行查询语句
    public function execute_dml($sql){
        $arr=array();
        $result=$this->mysqli->query($sql) or die($this->mysqli->error);
        if($result){
        while($row=$result->fetch_assoc()){
            //将查询结果封装到一个数组中,返回给方法调用处
            $arr[]=$row;
        }
        //释放查询结果资源
        $result->free();
        }
        return $arr;
    }

    //执行统计语句
    public function total($sql){
        $result = $this->mysqli->query($sql) or die($this->mysqli->error);
        return $result->num_rows;
     }

    //执行增加、删除、更新语句
    public function execute_dql($sql){
        $result=$this->mysqli->query($sql) or die($this->mysqli->error);
        if(!$result){
        return 0;//表示操作失败
        }else{
        if($this->mysqli->affected_rows>0){
            return 1;//操作成功
        }else{
            return 2;//没有受影响的行
        }
        }
    }
    }
?>
时间: 2024-08-25 08:53:19

php DBhelp的相关文章

C# - Dbhelp

dbhelp.cs using System; using System.Data; using System.Data.Common; using System.Configuration; public class DbHelper { private static string dbProviderName = ConfigurationManager.AppSettings["DbHelperProvider"]; private static string dbConnect

带事物处理的DBHelp和sql语句

DBHelp语句 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace 模拟ATM机转账 { static class DBHelp { public static bool IDUBySql(string sql) { bool fa = false; strin

DBHelp数据处理类

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using MyShoolModels; namespace MyShoolDAL { public class DBhelper { string conn = "Data Source=.;Initial

笔记8:winfrom连接数据库DBHelp

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data.Sql; 6 using System.Data.SqlClient; 7 using System.Data; 8 9 namespace 库存管理系统 10 { 11 class DBHelf 12 { 13 //增删改 14 public int sqlDS(Str

LigerUI一个前台框架增、删、改asp.net代码的实现

先上代码:前台代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">

欢迎进入MyKTV前后台点歌系统展示

一个项目,一分收获:一个项目,一些资源.Ktv项目也是一样的,所以我想分享我的收获,让你们获得你需要的资源. 一. 那MyKTV点歌系统具体的功能有哪些呢?我们就来看看吧! 1.MyKTV前台功能: 01.歌星点歌 .拼音点歌 .数字点歌 .类型选择 .金榜排行 02.切歌 .点歌 .重唱和退出 2.MyKTV后台功能: 01.歌手管理 .歌曲管理 .设置资源路径 02.新增歌手.歌曲 ,查询歌手.歌曲信息,设置歌曲路径和退出 二. 功能已经概括的差不多了,就让我们一起来看看MyKTV的项目吧

java操作数据库的通用的类

package cn.dao; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet;

连接Oracle数据库帮助类

连接Oracle数据库帮助类,就是把连接Oracle数据库的方法封装起来,只需要在其它页面调用就可,不需要重复写. import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import oracle.jdbc.driver.OracleDriver; /** * 数据库连接帮助类 * @author zql_pc * */ public class DbHelp { /* *

Select,Add,Update,Delete

//查询 public static DataSet select(string tableName,string whereStr) { string sql = "select * from "+tableName+whereStr; try { return DbHelp.ExecSql(sql); } catch (Exception e1) { return null; } } //删除单条 public static int delete (string tableName