0

PHP でアクティブ レコード パターンを実装しようとしています。次のコードがあります

<?php

// define 'MySQL' class
class users{
  private $result;

  public function __construct($host='localhost',$user='user',$password='password',$database='pruebalabo'){
    // connect to MySQL and select database
    if(!($conId = @mysql_connect("127.0.0.1","root",""))){
       throw new Exception('Error connecting to the server');
    }
    if(!mysql_select_db("pruebalabo",$conId)){
       throw new Exception('Error selecting database');
    }
  }

  // run SQL query
  public function query($query){
    if(!$this->result=mysql_query($query)){
      throw new Exception('Error performing query '.$query);
    }
  }
  // fetch one row
  public function fetchRow(){
    while($row=mysql_fetch_array($this->result)){
      return $row;
    }
    return false;
  }
  // fetch all rows
  public function fetchAll($table='users'){
     $this->query('SELECT * FROM '.$table);
     $rows=array();
     while($row=$this->fetchRow()){
        $rows[]=$row;
     }
     return $rows;
  }
  // insert row
  public function insert($params=array(),$table='default_table'){
     $sql='INSERT INTO '.$table.' ('.implode(',', array_keys($params)).') VALUES ('.implode("','",array_values($params)).')';
     $this->query($sql);
  }

}


try{
  // connect to MySQL and select a database
  $db=new users("host","root","","pruebalabo");
  $result=$db->fetchAll('users');
  foreach($result as $row){
     echo $row['firstname'].' '.$row['lastname'].' '.$row['email'].'<br />';
  }

  // connect to MySQL and select a database
  $db=new users("127.0.0.1","root","","pruebalabo");
  // insert new row into selected MySQL table
  $db->insert(array('firstname'=>'Sebas','lastname'=>'Guajardo','email'=>'tururus@domain.com'),'users');

}

catch(Exception $e){
  echo $e->getMessage();
  exit();
}

?>

ただし、コードを実行しようとすると、次の行でエラーが発生します

$db->insert(array('firstname'=>'Sebas','lastname'=>'Guajardo','email'=>'tururus@domain.com'),'users');

構文にエラーは見られません。例はこのページからほぼそのまま引用しました。私のデータベース (MySQL に実装) の名前は「pruebalabo」で、テーブルの名前は「users」です。

編集:指摘された間違いを修正しました。それでもエラーが発生します。

4

3 に答える 3

2

関数には変数$firstnameが含まれていますが、その変数は存在しません。配列値をリストimplode(',' , array_keys($params))に内破するときに、配列キーを列名として指定するつもりのようです。VALUES()

 public function insert($params=array(),$table='default_table'){
   // Pass the array keys as column names to match the VALUES().
   // I have surrounded them all in `backquotes` in the implode() here
   $sql="INSERT INTO ".$table." (`" . implode('`,`', array_keys($params)) . "`) VALUES ('".implode("','",array_values($params))."')";
   $this->query($sql);
 }

mysql_query()呼び出しの成功または失敗を常にチェックし、失敗した場合は の出力を調べますmysql_error()

注: 値が$paramsこのようにハードコーディングされていない場合は、値をクエリに渡す前に安全にエスケープしていると想定しています。

于 2012-10-05T18:06:18.823 に答える
1

以下のように挿入機能を変更します

public function insert($params=array(),$table='default_table'){
     $sql='INSERT INTO '.$table.' ('.implode(',', array_keys($params)).') VALUES ('.'\''.implode("','",array_values($params)).'\''.')';
     $this->query($sql);
  }
于 2012-10-05T18:18:41.037 に答える
1

この機能

public function insert($params=array(),$table='default_table'){
     $sql='INSERT INTO '.$table.' ('.$firstName.') VALUES ('.implode("','",array_values($params)).')';
     $this->query($sql);
  }

"$firstName" で定義されていない変数を使用し、指定する値が ('.$firstName.') で指定する列に対応するように注意してください。

関数に次のコードを使用します。問題は「'」文字の欠落にあるようです。

$sql= "INSERT INTO $table (".implode(',', array_keys($params))
 .") VALUES ('".implode("','",array_values($params))."')"; 
$this->query($sql);

よろしく

于 2012-10-05T18:08:45.430 に答える