2016-08-09 7 views
-1

こんにちは私はウェブサイトを作成していますが、dbからアイテムを選択するためのカスタム関数を作成したいのですが、私の問題は解決できません。dbから項目を選択する汎用関数を作成する

function select($select, $from, $where, $item) 
{ 
    global $db; 
    if ($where != "") 
    { 
     $pdoselect = $db->prepare("select :select from :from where :where = :where2"); 
     $pdoselect->bindParam(":select", $select); 
     $pdoselect->bindParam(":from", $from); 
     $pdoselect->bindParam(":where", $where); 
     $pdoselect->bindParam(":where2", $item); 
     $pdoselect->execute(); 
     foreach ($pdoselect as $return) 
     { 
      echo $return[" . $select . "]; 
     } 
    } else { 
     $pdoselect = $db->prepare("select :select from :from"); 
     $pdoselect->bindParam(":select", $select); 
     $pdoselect->bindParam(":from", $from); 
     $pdoselect->execute(); 
     foreach ($pdoselect as $return) 
     { 
      echo $return[" . $select . "]; 
     } 
    } 

} 
+1

あなたのクエリには ':'で始まるプレースホルダはありませんので、 'bindParam'呼び出しはどのようなものでしょうか? – Barmar

+1

テーブル名に ':placeholder'を使用することはできません。リテラル値を使用できる場所でのみ使用できます。 – Barmar

+0

私はそれを編集しました。申し訳ありません –

答えて

2

テーブル名とカラム名にプレースホルダを使用できないため、クエリのこれらの部分に対して通常の文字列置換を行う必要があります。比較する値には、WHERE句でプレースホルダを使用できます。

+0

私は多くの助けをしてくれてありがとうございました。 ) –

関連する問題