MrCG 2016-08-27 12:21 采纳率: 0%
浏览 1046

php测试mysql类失败,哪里错了?

运行index.php时一片空白,什么提示都没有;调试时发现mysql类中的select_db($db) 方法和后面的都未得到执行
大神帮忙看看 ,谢谢!

 <?php
//数据库类
class mysql extends db {
    private static $ins = NULL;
    private $conn = NULL;
    private $conf = array();

    protected function __construct() {
        $this->conf = conf::getIns();
        $this->connect($this->conf->host,$this->conf->user,$this->conf->pwd);
        $this->select_db($this->conf->db);
        $this->setChar($this->conf->char);
    }

    public function __destruct() {
    }

    public static function getIns() {
        if(!(self::$ins instanceof self)) {
            self::$ins = new self();
        }
        return self::$ins;
    }

    public function connect($h,$u,$p) {
        $this->conn =@mysql_connect($h,$u,$p);
        if(!$this->conn) {
            $err = new Exception('连接失败');
            throw $err;
        }
    }

    protected function select_db($db) {
        $sql = 'use '.$db;
        $this->query($sql);
    }

    protected function setChar($char) {
        $sql = 'set names ' . $char;
        return $this->query($sql);
    }

    public function query($sql) {

        $rs = mysql_query($sql,$this->conn);

        log::write($sql);

        return $rs;
    }

    public function autoExecute($table,$arr,$mode='insert',$where = ' where 1 limit 1') {
        /*    insert into tbname (username,passwd,email) values ('',)
        /// 把所有的键名用','接起来
        // implode(',',array_keys($arr));
        // implode("','",array_values($arr));
        */

        if(!is_array($arr)) {
            return false;
        }

        if($mode == 'update') {
            $sql = 'update ' . $table .' set ';
            foreach($arr as $k=>$v) {
                $sql .= $k . "='" . $v ."',";
            }
            $sql = rtrim($sql,',');
            $sql .= $where;

            return $this->query($sql);
        }

        $sql = 'insert into ' . $table . ' (' . implode(',',array_keys($arr)) . ')';
        $sql .= ' values (\'';
        $sql .= implode("','",array_values($arr));
        $sql .= '\')';

        return $this->query($sql);

    }

    public function getAll($sql) {
        $rs = $this->query($sql);

        $list = array();
        while($row = mysql_fetch_assoc($rs)) {
            $list[] = $row;
        }

        return $list;
    }

    public function getRow($sql) {
        $rs = $this->query($sql);

        return mysql_fetch_assoc($rs);
    }

    public function getOne($sql) {
        $rs = $this->query($sql);
        $row = mysql_fetch_row($rs);

        return $row[0];
    }

    // 返回影响行数的函数
    public function affected_rows() {
        return mysql_affected_rows($this->conn);
    }

    // 返回最新的auto_increment列的自增长的值
    public function insert_id() {
        return mysql_insert_id($this->conn);
    }


}

 <?php
//file:index.php 首页面
//所有用户直接访问到的这些页面必须先加载init.php
require('./include/init.php');
//测试数据库
$mysql=mysql::getIns();
var_dump($mysql);

 <?php
//配置文件

$_CFG=array();
$_CFG['host']='locallhost';
$_CFG['user']='root';
$_CFG['pwd']='hooray';
$_CFG['db'] = 'boolshop';
$_CFG['char'] = 'utf8';
?>
 <?php

/**
 file:conf.class.php
  配置文件读取类
**/
class conf{
 protected static $ins=null;
 protected $data=array();
 final protected function __construct(){
     //一次性配置文件信息,赋给data
   include(ROOT.'include/config.inc.php');
   $this->data=$_CFG;
 }
 final protected function __clone(){

 }
 public static function getIns(){
 if(self::$ins instanceof self){
     return self::$ins;
 }
 else{
 self::$ins=new self();
     return self::$ins;

 }
 }
// 用魔术方法获取data信息
public function __get($key){
      if(array_key_exists($key,$this->data)){
   return $this->data[$key];
      }else{
   return null;
       }

}
//用魔术方法,在运行时动态添加和更改配置

public function __set($key,$value)
{
   $this->data[$key]=$value;
}
}
$conf = conf::getIns();
?>
 <?php
// fiel:init.php 框架初始化
//初始化当前的路径
//echo substr(str_replace('\\','/',__FILE__),0,-8);
define('ROOT',str_replace ('\\','/',dirname(dirname(__FILE__)).'/'));
define('debug',true);
require(ROOT.'include/db.class.php');
require(ROOT.'include/mysql.class.php');
require(ROOT.'include/conf.class.php');
require(ROOT.'include/log.class.php');
require(ROOT.'include/lib_base.php');

//过滤参数
$_GET =_addslashes($_GET);
$_POST=_addslashes($_POST);
$_COOKIE=_addslashes($_COOKIE);

// 设置报错级别
  if(defined('DEBUG')){
    error_reporting(E_ALL);}
  else{
    error_reporting(0);}
  ?>
  • 写回答

1条回答 默认 最新

  • 泰 戈 尔 博客专家认证 2016-08-27 14:55
    关注

    你的mysql拓展打开了吗?

    没开的话先在php.ini文件中吧对应的extension前面的分号去掉。

    如果开了的话,建议使用debug模式来进行逐步调试,就一定可以找到错误信息的。

    评论

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?