类,该类在php中生成所有查询

类,该类在php中生成所有查询,php,mysql,class,oop,Php,Mysql,Class,Oop,我正在创建一个类,其中MySQL查询将自动生成,但我有一些问题 这是我的数据库类 <?php class Database { var $host="localhost"; var $username=""; Var $password=""; var $database=""; var $fr_query; var $row= array() ; public function connect() { $c

我正在创建一个类,其中MySQL查询将自动生成,但我有一些问题

这是我的数据库类

 <?php
    class Database {

    var $host="localhost";
    var $username="";    
    Var $password="";
    var $database="";
    var $fr_query;
    var $row= array() ;


public function connect() 
{
    $conn= mysql_connect($this->host,$this->username,$this->password);
    if(! $conn )
    {
    die('Could not connect: ' . mysql_error());
    }
}

public function db() 
{
    $conn_db = mysql_select_db($this->database);
    if(! $conn_db )
    {
        echo 'Could Not Connect the Database';
    }
}

public function run_query($sql)
{

 $run = mysql_query($sql);
    if(!$run)
    {
       throw new Exception("!!!!!Invalid query!!!!!!!"); 
    }

    $newId = mysql_insert_id();

    if($newId)
    {
        return $newId;
    }

    return true;
}


public function fetchRow($fr) 
{
        if($fr)
        {
            $run = mysql_query($fr);
            if($run)
            {
             return mysql_fetch_assoc($run);   
            }
        }
        return null;
}

function fetchAll($fr_query) 
{

        if($fr_query)
        {
        $run = mysql_query($fr_query); 
        if($run)
        {      
            $data=array();
            while($row=mysql_fetch_assoc($run))
            {
                $data[]=$row;
            }
            return $data;
        }
    }
    return null;   
    }
   }   
   $n = new Database();
   $n->connect();
   $n->db();
 ?>

这是我的test.php

 <?php
  include("database.php");
   class Model_Abstract
  {
   protected $_data  = array();
   protected $_tableName = null;
   protected $_primaryKey = null;

    public function getTableName()
    {                                                    
    return $this->_tableName;
    } 

    public function getPrimaryKey()
    {                                                    
    return $this->_primaryKey;
    }

     public function __set($key, $value = NULL)
     {
     $key = trim($key);
     if(!$key)
     {
        throw new Exception('"$key" should not be empty.');
     }

     $this->_data[$key] = $value;
      return $this;
    }

    public function __get($key)
    {
    $key = trim($key);
    if(!$key)
    {
        throw new Exception('"$key" should not be empty.');
    }

    if(array_key_exists($key, $this->_data))
    {
        return $this->_data[$key];    
    }

    return NULL;
  }

  public function insert()
  {
    print_r($this->_data); 


    $keyString = "`".implode("`,`", array_keys($this->_data))."`";
    $valueString = "'".implode("','", array_keys($this->_data))."'";

    echo $query  = "INSERT INTO `{$this->getTableName()}` ({$keyString}) VALUES ({$valueString})";
    $this->adpater()->run_query($query);

    echo 'Inserted';

}

public function setData($data)
{
    if(!is_array($data))
    {
        throw new Exception('"$data" should not be empty.');    
    }
    $this->_data = $data;
    return $this;
}



public function load($id, $key = null)
{    
    if(!is_int($id) && $id)
    {
        throw new Exception('"$id" should not be blank.');    
    }

    if($id)
    {
       echo $query = "SELECT * FROM `{$this->getTableName()}` WHERE `{$this->getPrimaryKey()}` = '{$id}'";
       $data[] = $this->adpater()->fetchRow($query); 


       $tabelName = $this->getTableName();

       foreach($data as &$_data)
       {
           print_r($_data);

           $object = new $tabelName();
           $object->setData($_data);
           $_data = $object;
       }
       print_r($data);
       return $this;
                    /*
       $query = "SELECT * FROM `{$this->getTableName()}` WHERE `{$this->getPrimaryKey()}` = '{$id}'";
       $this->_data = $this->adpater()->fetchRow($query); 
       return $this;  */
    }
}


public function loadAll()
{    

       $query = "SELECT * FROM `{$this->getTableName()}`";
       $data[] = $this->adpater()->fetchAll($query); 
       return $data;
    }

public function delete($id, $key = null)
{    
    if(!is_int($id) && $id)
    {
        throw new Exception('"$id" should not be blank.');    
    }

    if($id)
    {
       echo $query = "DELETE FROM `{$this->getTableName()}` WHERE `{$this->getPrimaryKey()}` = '{$id}'";
       $data[] = $this->adpater()->run_query($query); 


       $tabelName = $this->getTableName();

       $msg = 'Deleted Successfully....';
       return $msg;

    }
}

 public function update()
{
    print_r($this->_data); 


    $keyString = "`".implode("`,`", array_keys($this->_data))."`";
    $valueString = "'".implode("','", array_keys($this->_data))."'";

    echo $query  = "UPDATE`{$this->getTableName()}` SET ({$keyString}) = ({$valueString}) WHERE `{$this->getPrimaryKey()}` = '{$id}'";
    $this->adpater()->run_query($query);

    echo 'Updated';

}


   public function adpater()
   {
    return new Database();                      
   }
}

class Product extends Model_Abstract
{
  protected $_tableName = 'product';
  protected $_primaryKey = 'product_id';

}

$product = new Product();

 echo $product->name; 



 $product->insert();

 print_r($product);


$product = new Product();
$product->name = 'Nokia Lumia';
$product->description = 'Windows';
$product->price = '15000';
$product->quantity = '12';
$product->sku = 'x2';
$product->status = '2';
$product->created_date = '0000-00-00 00:00:00';
$product->updated_date = ' ';
?>

相同的行,相同的值。也许你的意思是

$keyString = "`".implode("`,`", array_keys($this->_data))."`";
$valueString = "'".implode("','", $this->_data) ."'";
这将获取
$keyString
的数组键和
$valueString
的数组值


折旧警告

mysql.*
是不推荐使用的函数。使用或


警告


这门课并不能保护你免受伤害。

你期待什么?对于
keyString
valueString
可能应该是
array\u keys
array\u values
?不要再使用
mysql\u*
函数,因为它们已被弃用,并且将在PHP的下一个版本中删除。使用
PDO
mysqli
代替。在valueString中,我需要值部分作为数组,但它是作为唯一的键出现的。您可能希望对您的值使用
array\u values
。否则,除了使用
mysql\u
函数之外,您还得到了基本的想法,尽管通常使用的术语是create、read(因为)加载而不是插入。您能指导我进行更新查询吗,因为在更新中,每个列的名称都跟随其值….???插入产品(
名称
说明
价格
数量
库存单位
状态
创建日期
更新日期
)值('Nokia Lumia'、'Windows'、'15000'、'12'、'x2'、'2'、'0000-00-00:00:00'、'')致命错误:未捕获异常'exception'带有消息'!!!!!无效查询!!!!!'在/var/www/database.php:36堆栈跟踪:#0/var/www/test.php(56):数据库->运行查询('INSERT-INTO-pro…')#1/var/www/test.php(204):Model\u-Abstract->INSERT()#2{main}在线输入/var/www/database.php36@PankajYadav读取代码:由于查询返回false,因此引发此异常。获取mysql错误(使用
mysql[i]\u error()
from)然后在你的
异常
中打印它。另外,我认为你应该为该异常创建一个专用类,而不是使用泛型
异常
类。谢谢,我会记住的
$keyString = "`".implode("`,`", array_keys($this->_data))."`";
$valueString = "'".implode("','", $this->_data) ."'";