2017-11-20 12 views
1

PHPスクリプトで "find"というメソッドを用意してデータベース検索を実行しました。データベース内に何かを検索しているので、 "bool(false)"。私はどこでミスをしたのか分かりませんでした。これはメソッド "find"と "findfirst"のコードです(findfirstの場合はfindメソッドと同じ概念です)。PHPスクリプトのデータベースとのやりとりで予期しない結果が発生しました

protected function _read($table,$params=[]) 
{ 
    $conditionString= ''; 
    $bind=[]; 
    $order=''; 
    $limit=''; 
//conditions 
if(isset ($params['conditions'])) 
{ 
    if(is_array($params['conditions'])) 
    { 
    foreach($params['conditions'] as $condition) 
    { 
     $conditionString.=' ' . $condition . ' AND'; 
    } 
    $conditionString=trim($conditionString); 
    $conditionString=rtrim($conditionString,' AND'); 

    } 

    else { 
    $conditionString= $params['conditions']; 

    } 

if($conditionString !=''){ 
$conditionString=' Where' . $conditionString; 
} 

} 
//bind 
if (array_key_exists('bind',$params)) 
{ 
    $bind=$params['bind']; 
} 
//order 
if (array_key_exists('order',$params)) 
{ 
    $order=' ORDER BY ' . $params['order']; 
} 


//limit 
    if (array_key_exists('limit',$params)) 
{ 
    $limit=' LIMIT ' . $params['limit']; 
} 

$sql="SELECT * FROM {$table}{$conditionString}{$order}{$limit}"; 
if ($this->query($sql,$bind)) 
{ 
    if(!count($this->_result)) return false; 
    return true; 
} 
return false; 


} 



public function find ($table ,$params=[]) 
{ 
if ($this->_read($table,$params)){ 
    return $this->results(); 
} 
return false; 
} 

public function findfirst ($table ,$params=[]) 
{ 
    if ($this->_read($table,$params)){ 
    return $this->first(); 
} 
return false; 

} 

これは私が「find」というメソッドを呼び出すhome.phpのコードです。

$usersU=$db->find('users',[ 

'conditions'=>"username = ?", 
'bind'=>['kh'], 
'order'=>"username, password", 
'limit'=>'' 
]); 

これはメソッド "結果"と "最初"です。

public function results(){ 

    return $this->_result; 
} 

public function first(){ 

    return (!empty($this->_result)) ? $this->_result[0] : []; 
} 

これはクエリメソッドです。

public function query ($sql, $params = []) 
 
{ 
 
$this->_error = false; 
 
if ($this->_query = $this->_pdo->prepare($sql)) 
 
{ 
 
    $x=1 ; 
 
    if (count($params)) 
 
{ 
 
foreach ($params as $param) 
 
{ 
 
    $this->_query->bindValue($x,$param); 
 
    $x++; 
 
} 
 

 
} 
 

 
if($this->_query->execute()) 
 
{ 
 

 
    $this->_result = $this->_query->fetchALL(PDO::FETCH_OBJ); 
 
    $this->_count = $this->_query->rowCount(); 
 
    $this->_lastInsertID =$this->_pdo->lastInsertId(); 
 

 
} 
 
else 
 
{ 
 
    $this->_error= true; 
 
} 
 

 
} 
 
return $this; 
 

 
}

+0

を追加することで解決しましたクエリ?あなたは予期せぬ結果は何ですか? – shushu304

+0

"bool(false)"これは私のデータベースに実際に存在するものを探しているので予期せぬ結果です。 –

+0

最終的なクエリは何ですか? PDOを使用しているエラーメッセージを確認してください。 – shushu304

答えて

0

それが最終的には何をしている...私たちは、コードが表示されますので、findメソッドを追加し、いくつかのスペースに{$表} {$ conditionString} {$順序} {$限度}

0

以下キーワードPASSWORD(MySQLは予約キーワードに関して大文字と小文字を区別しないで)は、MySQL Reserved Keywordであり、そのためには、識別子として許可されるように適切に引用されなければなりません。

$usersU=$db->find('`users`',[  
'conditions'=>'`username` = ?', 
'bind'=>['kh'], 
'order'=>'`username`, `password`', 
'limit'=>'' 
]); 

次のことを試してみてください。クエリを作成するifステートメントで$params['limit']$params['order']を空にしないでください。

protected function _read($table,$params=[]) 
{ 
    $conditionString= ''; 
    $bind=[]; 
    $order=''; 
    $limit=''; 
//conditions 
if(isset ($params['conditions'])) 
{ 
    if(is_array($params['conditions'])) 
    { 
    foreach($params['conditions'] as $condition) 
    { 
     $conditionString.=' ' . $condition . ' AND'; 
    } 
    $conditionString=trim($conditionString); 
    $conditionString=rtrim($conditionString,' AND'); 

    } 

    else { 
    $conditionString= $params['conditions']; 

    } 

if($conditionString !=''){ 
$conditionString=' Where ' . $conditionString; 
} 
} 
//bind 
if (array_key_exists('bind',$params)) 
{ 
    $bind=$params['bind']; 
} 
//order check that $params['order'] is not empty also 
if (array_key_exists('order',$params) && !empty($params['order'])) 
{ 
    $order=' ORDER BY ' . $params['order']; 
} 

//limit check that $params['limit'] is not empty also 
    if (array_key_exists('limit',$params) && !empty($params['limit'])) 
{ 
    $limit=' LIMIT ' . $params['limit']; 
} 

$sql="SELECT * FROM {$table}{$conditionString}{$order}{$limit}"; 
if ($this->query($sql,$bind)) 
{ 
    if(!count($this->_result)) return false; 
    return true; 
} 
return false;  
} 



public function find ($table ,$params=[]) 
{ 
if ($this->_read($table,$params)){ 
    return $this->results(); 
} 
return false; 
} 

public function findfirst ($table ,$params=[]) 
{ 
    if ($this->_read($table,$params)){ 
    return $this->first(); 
} 
return false; 

} 
+0

それは引用符の問題ではありません私はerrorInfo()を使用して私にこのエラーを示すSQL文の問題だと思います –

+0

Array([0] => 42000 [1 ] => 1064 [2] => SQL構文に誤りがあります。MariaDBサーバーのバージョンに対応したマニュアルをチェックして、正しい構文を確認してください。 '1行目付近で使用してください) –

+0

@miramohamedこれは解析エラーです一重引用符と二重引用符を組み合わせてクエリを作成しています。 –

関連する問題