zoukankan      html  css  js  c++  java
  • SqlHelper.class.php

    /**
    燕十八 公益PHP培训
    课堂地址:YY频道88354001
    学习社区:www.zixue.it
    **/

    <?php
    /*************************************************************
    MySql类封装:
    首先连接数据库,需要有参数
    参数如何传?
        1、可以用配置文件
        2、可以通过构造函数传参
    **************************************************************/
    class SqlHelper{
        private $host;
        private $user;
        private $pwd;
        private $dbName;
        private $charset;
        private $conn = null; //保存连接资源

        public function __construct(){
            $this->host  = 'localhost';
            $this->user = 'root';
            $this->pwd = '123456';
            $this->dbName = 'test';
            $this->charset='utf8';
            //连接
            $this->connect($this->host, $this->user, $this->pwd);
            //选库
            $this->switchDb($this->dbName);
            //设置字符集
            $this->setChar($this->charset);
        }
        
        //负责连接
        private function connect($host, $user, $pwd){
            $conn = mysql_connect($host, $user, $pwd);
             if (!$conn) {
                echo "Unable to connect to DB: " . mysql_error();
                exit;
            }
            $this->conn = $conn;
        }
       
        //选库
        public function switchDb($db){
            $sql = 'use ' . $db; //注意user 和 ' 有一个空格
            $this->query($sql);
        }
        
        //设置字符集
        public function setChar($char){
            $sql = 'set names ' . $char;
            $this->query($sql);
        }

        //关闭连接
        public function close(){
            mysql_close($this->conn);
        }

        //负责发送sql查询
        public  function query($sql){
            $result = mysql_query($sql, $this->conn);
            return $result;
        }
        
        //获取多行多列的select结果
        public function getAll($sql){
            $list  = array();
            $result = $this->query($sql);
            if(!$result)
                return false;
            while($row= mysql_fetch_assoc($result)){
                $list[] = $row;
            }
            return $list;
        }

        //获取一行数据 常用于 聚合函数
        public function getRow($sql){
            $result = $this->query($sql);
            if(!$result)
                return false;
            $row= mysql_fetch_assoc($result);
            return $row;
        }

         //获取一个值
        public function getOne($sql){
            $result = $this->query($sql);
            if(!$result)
                return false;
            $row= mysql_fetch_row($result);
            return $row[0];
        }
        
    }

    //$mysql = new SqlHelper();
    /*
    var_dump($mysql);
    $sql = "insert goods values(1121, 4, '苹果')";
    $mysql->query($sql);

    $sql = 'select * from goods';
    $list = $mysql->getAll($sql);
    var_dump($list);


    $sql = 'select * from goods where goods_id=4';
    $list = $mysql->getRow($sql);
    var_dump($list);


    $sql = 'select count(*) from goods';
    $list = $mysql->getOne($sql);
    var_dump($list);
    */

  • 相关阅读:
    express 项目前后台公用样式 /static/js/bootstrap.min.js
    判断滚动条是否到了底部
    JS如何判断滚动条是否滚到底部
    移动端touch触屏滑动事件、滑动触屏事件监听!
    JS事件监听手机屏幕触摸事件 Touch
    nodejs mysql 连接数据库
    nodejs route的简单使用
    nodejs jade 模板 引擎的使用方法
    nodejs 模板引擎ejs的简单使用(3)
    nodejs 模板引擎ejs的简单使用(2)
  • 原文地址:https://www.cnblogs.com/qintangtao/p/2764526.html
Copyright © 2011-2022 走看看