<?php
/*
 * Created on 2010-10-21
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class mysql{  private $db_host;  //主机地址
  private $db_user;
  private $db_pwd;
  private $db_database;   //定义数据库
  private $conn;    //连接数据库
  private $coding;    //字符编码 private $bulletin = true; //是否开启错误记录
private $show_error = true; //测试阶段,显示所有错误 具有安全隐患,默认关闭
private $is_error = false; //发现错误是否立即终止 默认为TRUE建议不启用
//构造函数
public function __construct($db_host,$db_user,$db_pwd,$db_database,$conn,$coding){        //初始化函数
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pwd = $db_pwd;
$this->db_database = $db_database;
$this->conn  =  $conn;
$this->coding  =  $coding; $this->connect();
}
//连接数据库
  public function connect(){
  if($this->conn == "pconn"){
  //永久连接
  $this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pwd);
  }else{
  //即使连接
$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pwd);
  }  if(!mysql_select_db($this->db_database,$this->conn)){
  if($this->show_error){
  $this->show_error("数据库不可用:",$this->db_database);
  }
  }
  mysql_query("SET NAMES $this->coding");
  }
//数据库执行语句 可执行语句 增删改查 public function query($sql){
if($sql == ""){
$this->show_error("SQl语句错误","SQl语句为空");
}
$this->sql=$sql; $result = mysql_query($this->sql,$this->conn);
} //创建数据库
public function crate_database($database_name){
$database   =  $database_name;
    $sqlDatabase ='create database' .$database;
$this->query($sqlDatabase);
}
//将数据库 和系统中的数据库分开
public function show_database(){
$rs = $this->query("show_database");
echo "现有数据库".$amount=$this->db_num_rows($rs);/// echo "<br/>";
$i = 1;
while($row = $this->fetch_array($rs)){
echo "$i $row[$Database]";
echo "<br/>";
$i++;
}
} //以数组形式  返回主机中所有数据库名
public function databases(){
$rsPtr = mysql_list_dbs($this->conn);
$i = 0;
$cnt = mysql_num_rows($rsPtr);
while($i < $cnt){
$rs[] = mysql_db_name($rsPtr,$i);
$i++;
}
return $rs;
} //查询数据库中所有的表
public function show_table($database_name){
$rs = $this->query("show tables");
echo "现有数据库" .$amount = $this->db_num_rows($rs);//
echo "<br />"; $i= 1;
while($row = $this->fetch_array($rs)){
$columnName = "Tables_in_" . $database_name;
echo "$i $row[$columnName]";
echo "<br />";
$i++;
}
}
//取得结果数据 public function mysql_result_li($str){
return mysql_result($str);//
} //取得记录集
public function fetch_array(){
return mysql_fetch_array($this->result);
}
//获取关联数组
public function fetch_assoc(){
return mysql_fetch_assoc($this->result);
} //获取数字索引
public function fetch_row(){
return mysql_fetch_row($this->result);
} //获取对象数组
public function fetch_Object(){
return mysql_fetch_Object($this->result);
} //简化查询select
public function findall($table){
$this->query("SELECT * FROM $table");
} //简化查询select
public function select($table,$columnName = "*",$condition = '',$debug = ''){
$condition = $condition ? 'where'.$condition : NULL;
if($debug){
echo "SELECT $columnName FROM $table $condition";
}else{
$this->query("SELECT $columnName FROM $table $condition");
}
} //简化删除del
public function delete($table,$condition,$url = ''){
if($this->query("DELETE FROM $table WHERE $condition")){
if(!empty($url)){
$this->Get_admin_msg($url,'删除成功');
}
}
} //简化插入insert
public function insert($table,$columnName,$value,$url=''){
if($this->query("INSERT INTO $table($columnName) UALUES($value)")){
if(!empty($url)){
$this->Get_admin_msg($url,'添加成功');
}
}
}
 } //$db = new mysql($mydbhost, $mydbuser,$mydbpw, $mydbname,ALL_PS,$uncode); //连接数据库?>

解决方案 »

  1.   

    //$row = $db->fetch_array($query);
    执行这句报错 、、、
      

  2.   


    //取得记录集
        public function fetch_array(){
            return mysql_fetch_array($this->result);
        }你这儿写的方法并没有参数传递
    你调用的时候怎么有参数了?
    你这儿的$this->result
    我发现并没有方法对其进行赋值,在执行 fetch_array之前  要执行对$this->result进行赋值的方法
      

  3.   

        public function query($sql){
            if($sql == ""){
                $this->show_error("SQl语句错误","SQl语句为空");
            }
            $this->sql=$sql;        $result = mysql_query($this->sql,$this->conn);
        }这里的$result 改成 $this->result;没很细看其他代码。。
      

  4.   

    你在调用fetch_array方法的之前调用 query方法了么
      

  5.   


    //$query = $db->query("");
     $query = $db->query("SELECT * FROM `p_admin`");
     $row = $db->fetch_array($query);
      

  6.   


    <?php
    /*
     * Created on 2010-10-21
     *
     * To change the template for this generated file go to
     * Window - Preferences - PHPeclipse - PHP - Code Templates
     */
     class mysql{     private $db_host;  //主机地址
         private $db_user;
         private $db_pwd;
         private $db_database;   //定义数据库
         private $conn;    //连接数据库
         private $coding;    //字符编码    private $bulletin = true; //是否开启错误记录
        private $show_error = true; //测试阶段,显示所有错误 具有安全隐患,默认关闭
        private $is_error = false; //发现错误是否立即终止 默认为TRUE建议不启用
        private $result ;//储存结果集
        //构造函数
        public function __construct($db_host,$db_user,$db_pwd,$db_database,$conn,$coding){        //初始化函数
            $this->db_host        =    $db_host;
            $this->db_user        =    $db_user;
            $this->db_pwd        =    $db_pwd;
            $this->db_database    =    $db_database;
            $this->conn         =     $conn;
            $this->coding         =     $coding;        $this->connect();
        }
    //连接数据库
         public function connect(){
             if($this->conn == "pconn"){
                 //永久连接
                 $this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pwd);
             }else{
                 //即使连接
                $this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pwd);
             }         if(!mysql_select_db($this->db_database,$this->conn)){
                 if($this->show_error){
                     $this->show_error("数据库不可用:",$this->db_database);
                 }
             }
         mysql_query("SET NAMES $this->coding");
         }
    //数据库执行语句 可执行语句 增删改查    public function query($sql){
            if($sql == ""){
                $this->show_error("SQl语句错误","SQl语句为空");
            }
            $this->sql=$sql;        $this->result = mysql_query($this->sql,$this->conn);
        }    //创建数据库
        public function crate_database($database_name){
            $database   =  $database_name;
            $sqlDatabase ='create database' .$database;
            $this->query($sqlDatabase);
        }
        //将数据库 和系统中的数据库分开
        public function show_database(){
            $rs = $this->query("show_database");
            echo "现有数据库".$amount=$this->db_num_rows($rs);///        echo "<br/>";
            $i = 1;
            while($row = $this->fetch_array($rs)){
                echo "$i $row[$Database]";
                echo "<br/>";
                $i++;
            }
        }    //以数组形式  返回主机中所有数据库名
        public function databases(){
            $rsPtr = mysql_list_dbs($this->conn);
            $i = 0;
            $cnt = mysql_num_rows($rsPtr);
            while($i < $cnt){
                $rs[] = mysql_db_name($rsPtr,$i);
                $i++;
            }
            return $rs;
        }    //查询数据库中所有的表
        public function show_table($database_name){
            $rs = $this->query("show tables");
            echo "现有数据库" .$amount = $this->db_num_rows($rs);//
            echo "<br />";        $i= 1;
            while($row = $this->fetch_array($rs)){
                $columnName = "Tables_in_" . $database_name;
                echo "$i $row[$columnName]";
                echo "<br />";
                $i++;
            }
        }
        //取得结果数据    public function mysql_result_li($str){
            return mysql_result($str);//
        }    //取得记录集
        public function fetch_array(){
            return mysql_fetch_array($this->result);
        }
        //获取关联数组
        public function fetch_assoc(){
            return mysql_fetch_assoc($this->result);
        }    //获取数字索引
        public function fetch_row(){
            return mysql_fetch_row($this->result);
        }    //获取对象数组
        public function fetch_Object(){
            return mysql_fetch_Object($this->result);
        }    //简化查询select
        public function findall($table){
            $this->query("SELECT * FROM $table");
        }    //简化查询select
        public function select($table,$columnName = "*",$condition = '',$debug = ''){
            $condition = $condition ? 'where'.$condition : NULL;
            if($debug){
                echo "SELECT $columnName FROM $table $condition";
            }else{
                $this->query("SELECT $columnName FROM $table $condition");
            }
        }    //简化删除del
        public function delete($table,$condition,$url = ''){
            if($this->query("DELETE FROM $table WHERE $condition")){
                if(!empty($url)){
                    $this->Get_admin_msg($url,'删除成功');
                }
            }
        }    //简化插入insert
        public function insert($table,$columnName,$value,$url=''){
            if($this->query("INSERT INTO $table($columnName) UALUES($value)")){
                if(!empty($url)){
                    $this->Get_admin_msg($url,'添加成功');
                }
            }
        }
     } $db = new mysql($mydbhost, $mydbuser,$mydbpw, $mydbname,ALL_PS,$uncode); //连接数据库
     $db->query("SELECT * FROM `p_admin`");
     $db->fetch_array();//这儿不要有参数?>
    像我这样调用呢》?
      

  7.   


    $row=$db->fetch_array();//这儿不要有参数
    //输出$row
      

  8.   

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in 
      

  9.   


    $db = new mysql($mydbhost, $mydbuser,$mydbpw, $mydbname,ALL_PS,$uncode); //连接数据库
     $db->query("SELECT * FROM `p_admin`");
     $row=$db->fetch_array();//这儿不要有参数这样也有这个错误??
    之前报错是什么错误?
      

  10.   

    //数据库执行语句 可执行语句 增删改查    public function query($sql){
            if($sql == ""){
                $this->show_error("SQl语句错误","SQl语句为空");
            }
            $this->sql=$sql;        $this->result = mysql_query($this->sql,$this->conn);
        }
      

  11.   


    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in 
      

  12.   

    这种基类有必要自己再完全重写一个吗?
    看看PHP开源的项目,找一个工具类过来不就行了吗、