2016-06-18 6 views
-2

登録データをデータベースに送信するPHPでログイン/登録コードを作成しようとしています。私はexecute()メソッドを呼び出しますが、何らかの理由で動作しません。このメソッドがデータベースにSQL文を送信すると、何が起こるはずですか?私は何を見落としていますか?エラーメッセージを取得またはそうPDO::ERRMODE_EXCEPTIONからPDO optionPDO::ATTR_ERRMODEを設定することが望ましいPHP PDO :: execute文は実行されません

+1

質問を明確にするために質問を編集してください。デバッグの助けを求める質問(**はなぜこのコードが動作しないのですか?**)は、質問自体に*必要な動作、*特定の問題またはエラー*、* *それを再現するのに必要な最短コード* *。 **明確な問題文**のない質問は他の読者には役に立たない。参照:[最小限で完全で検証可能な例の作成方法](http://stackoverflow.com/help/mcve) –

+0

「それはうまくいかない」ことについて詳しく説明できますか? –

+0

なぜその関数がすでにそれ自身の上に存在するときに、queryと呼ばれる関数を作成していますか? –

答えて

0

使用PDO::errorInfo()としての機能と呼ばれている

public function query($sql, $params= array()) { 
    /* query takes 2 paramaters, $sql which is a prepared sql statement, and params, which holds the value of the fields to be inserted into db */ 
    $this->_error = false; // field to determine if there is an error 
    $this->_query = $this->_pdo->prepare($sql); // $_query is another field holding the query value 

     $x=1; 
     if(count($params)){ //count is a function to determine if the paramater has a value 
      foreach($params as $param) { 
       $this->_query->bindValue($x, $param); 
       $x++; 
      } 
     } 
     if($this->_query->execute($sql)){ 
      $this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ); 
      $this->_count = $this->_query->rowCount(); // this is the block that is desired to be executed 
     } else { 
      $this->_error = true; // This is the block that is being executed 
     } 
    return $this; 
} 

Iは、データベースへの接続問題がない検証、およびクエリ内の他の方法() PDOはエラーが発生した場合に例外をスローします。

+0

OK、私はそれを走らせましたが、私は言語に慣れていないので、これが意味することを実際に理解していません - > PDO :: errorInfo():Array([0] => 21S01 [1] => 1136 [ 2] =>列の数が行1の値の数と一致しない – tornadoriley

+0

これは、SQL文で使用している量をカバーするには、バインド値が多すぎるか不足していることを意味します。 – Rasclatt

1

pdo実行パラメータはクエリではない可能性があります。実行時には、バインド配列のみが許可されます。試してみてください:あなたが手動でクエリを試みなければならないすべての

public function query($sql, $params= array()) { 
    /* query takes 2 paramaters, $sql which is a prepared sql statement, and params, which holds the value of the fields to be inserted into db */ 
    $this->_error = false; // field to determine if there is an error 
    $this->_query = $this->_pdo->prepare($sql); // $_query is another field holding the query value 

     $x=1; 
     if(count($params)){ //count is a function to determine if the paramater has a value 
      foreach($params as $param) { 
       $this->_query->bindValue($x, $param); 
       $x++; 
      } 
     } 
     if($this->_query->execute()){ // changed execute($sql) to execute() 
      $this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ); 
      $this->_count = $this->_query->rowCount(); // this is the block that is desired to be executed 
     } else { 
      $this->_error = true; // This is the block that is being executed 
     } 
    return $this; 
} 
0

まず、

よう
$db = new PDO("mysql:host=somehost;dbname=somedb","user","pass"); 
$stmt = $db->prepare("insert into table (a,b,c) values('1','2','3')"); 
$stmt ->execute(); 

これが成功を取得している場合は、あなたが結合データを試してみてください。 私はいつもエラーから抜け出す方法を得ます。

関連する問題