mysqli的方法测试小结

<?php

class MysqlController extends ControllerBase
{
    public $config = array();
    public $mysql = NULL;
    
    public function initialize(){
        $this->config = array(
            'host'=>'localhost:3306',
            'username'=>'daokrdb',
            'password'=>'123456',
            'dbname'=>'daokrdb',
        );
    }
    public function indexAction()
    {
        // 创建连接
        $mysql = new mysqli($this->config['host'], $this->config['username'], $this->config['password'], $this->config['dbname']);
        /* change db to world db */
        //$mysqli->select_db("world"); //可以利用select_db 改变数据库
        // Check connection
        if ($mysql->connect_error) {
            die("连接失败: " . $mysql->connect_error);
        }else{
            $thread_id = $mysql->thread_id;
            echo "mysql 的主机信息:".$mysql->host_info."<br/>";
            echo "mysql 查看服务器信息:".$mysql->server_info."<br/>";
            echo "mysql 获取最近一次数据库查询的信息:".$mysql->info."<br/>";
            echo "mysql 获取当前系统状态:".$mysql->stat()."<br/>";
            echo "mysql 是否线程安全:".$mysql->thread_safe()."<br>";
            echo "mysql 返回当前连接的线程ID:".$thread_id."<br>";
            $this->mysql = $mysql;
        }
        /* 设置客户端字符集 */
        if (!$mysql->set_charset("utf8")) {
            echo("设置utf8错误: ".$mysql->error."<br>");
        } else {
            echo("当前已设置的字符集:".$mysql->character_set_name()."<br>");
        }
        
        /* kill 当前连接线程 */
        //$mysql->kill($thread_id);
        $this->insert();
        $this->select();
        $this->find();
        $this->delete();
        $this->update();
        
        
        $mysql->close();
    }
    function select(){
        $sql = "SELECT t.* FROM t_users t";
        $result = $this->mysql->query($sql);
        if($result->num_rows>0){
            while ($row = $result->fetch_assoc())
            {
                echo "编号:".$row['id']." 标题:".$row['title']."<br>";
            }
            $result->close();
        }
    }
    //
    function insert(){
        $sql = "INSERT INTO t_users (title, author,submission_date) VALUES ('John', 'Doe', NOW())";
        
        if ($this->mysql->query($sql) === TRUE) {
            echo "新记录插入成功<br/>";
        } else {
            echo "Error: " . $sql . "<br>" . $this->mysql->error;
        }
        $name = "学习 PHP";
        $query = "SELECT title FROM t_users WHERE title=?";
        $binduser = $this->mysql->prepare($query); 
        if($binduser){
            $binduser->bind_param('s', $name);//i - integer(整型)d - double(双精度浮点型)s - string(字符串)b - BLOB(binary large object:二进制大对象)
            $binduser->execute();
            $binduser->bind_result($title); 
            $binduser->fetch();
            echo "bind_result 查询成功:".$title."<br/>";
            $binduser->close();
        }
    }
    //
    function delete(){
        $sql = 'delete from t_users where title="d" ';
        $result = $this->mysql->query($sql);
        if($result){
            echo "删除成功<br>";
        }else{
            echo "Error: " . $sql . "<br>" . $this->mysql->error;
        }
    }
    //
    function update(){
        $sql = 'update t_users set title="PHP webs" where id=31';
        $result = $this->mysql->query($sql);
        if($result){
            echo "更新成功<br>";
        }else{
            echo "Error: " . $sql . "<br>" . $this->mysql->error;
        }
    }

    
}

猜你喜欢

转载自www.cnblogs.com/wanglijun/p/8856739.html