php mysql工具类

方式一:
<?php

class SqlTool
{
    private $conn = null;
    private $host = "localhost";
    private $username = "root";
    private $password = "254416";
    function __construct()
    {
        $this->conn = mysql_connect($this->host, $this->username, $this->password);
        if (!$this->conn)
        {
            die("数据库链接失败" . mysql_error());
        }
        //选择数据库
        mysql_select_db("test") or die("数据库不存在" . mysql_error());
        //设置编码
        mysql_set_charset("utf8", $this->conn);
        //mysql_query("set names utf8");
    }
    function execute_dql($sql)
    {
        $res = mysql_query($sql, $this->conn);
        if (!$res)
        {
            die("数据查询失败");
        } else
        {
            return $res;
        }
    }
    function execute_dml($sql)
    {
        $res = mysql_query($sql, $this->conn);
        if (!$res)
        {
            echo mysql_error();
            return - 1;
        } else
        {
            if (mysql_affected_rows($this->conn) > 0)
            {
                return 1;
            } else
            {
                return 0;
            }
        }
    }
}

?>
方式二:通过mysqli扩展库操作mysql 
<?php

class SqlTool
{
    private $host = "localhost";
    private $username = "root";
    private $password = "254416";
    private $db = "test";
    private static $mysqli = null;
    function __construct()
    {
        //新建链接
        self::$mysqli = new MySQLi($this->host, $this->username, $this->password, $this->
            db);
        if (self::$mysqli->connect_error)
        {
            die("数据库连接失败" . self::$mysqli->connect_error);
        }
        //设置编码
        self::$mysqli->set_charset("utf8");
    }
    function execute_dql($sql)
    {
        $res = self::$mysqli->query($sql);
        return $res;
    }
    function queryfiledname($sql)
    {
        $filedname = self::$mysqli->query($sql);
        return $filedname;
    }
    function execute_dml($sql)
    {
        $b = self::$mysqli->query($sql);
        if (!$b)
        {
            return - 1;
        } else
        {
            if (self::$mysqli->affected_rows > 0)
            {
                return 1;
            } else
            {
                return 0;
            }
        }
    }
}

?>
结语:
<?php

header("Content-type: text/html;charset=utf-8");
//建立链接
$mysqli = new MySQLi("localhost", "root", "254416", "test");
$sql = "insert into user (username,password,email,address,age) values (?,?,?,?,?)";
//设置编码
$mysqli->query("SET NAMES utf8");
//数据库预处理
$mysqli_stmt = $mysqli->prepare($sql) or die("数据库预处理失败");
//绑定参数
$name = "小倩";
$password = "123";
$email = "xiaoqian@suhu.com";
$address = "郑州";
$age = 20;
$mysqli_stmt->bind_param("ssssi", $name, $password, $email, $address, $age) or
    die("绑定参数失败");
//执行
$b = $mysqli_stmt->execute();
if (!$b)
{
    die("预处理执行失败");
}
echo "添加成功";
$mysqli_stmt->close();

?> 


<?php

header("Content-type: text/html;charset=utf-8");
$mysqli = new MySQLi("localhost", "root", "254416", "test");
$mysqli->query("set names utf8");
$sql = "select * from user where id>?";
$mysqli_stmt = $mysqli->prepare($sql) or die("数据库预处理失败");
$id = 20;
$mysqli_stmt->bind_param("i", $id) or die("参数绑定失败");
$mysqli_stmt->bind_result($id, $username, $password, $email, $address, $age) or ("绑定结果集失败");
$b = $mysqli_stmt->execute();
if (!$b)
{
    die("数据库预处操作理失败");
}
while ($mysqli_stmt->fetch())
{
    echo "--$id--$username";
}
$mysqli_stmt->free_result();
$mysqli_stmt->close();
$mysqli->close();

?>


转载于:https://my.oschina.net/oppo4545/blog/210084

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值