2016-06-20 5 views
-1

私は問題があり、見て、スタックオーバーフローサイトで多くのソリューションを見つけた しかし、私は自分のコードでそれを試しても動作していないようです。誰かが私のコードに何が間違っているかを知るのを助けることを望んでいました。どこで注文IDが動作しない

スタックオーバーフローのソリューション

mysql query with where and order by take long time

のいくつかの私は、次のようにしかし、応答は常に、ステータス= $状態データベースからレコードを選択しようとしています:

警告:/user/khloudamer/Documents/Websites/BusinessDoorFinal2/index.php on line 98のforeach()引数が無効です。

次のよう

方法は次のとおりです。私は

(SELECT * FROM application where status = {$status} order by id desc limit 0,30') 
(SELECT * FROM application where status = '$status' order by id desc limit 0,30') 

            AND 
SELECT * FROM application where status = $status order by id desc limit 0,30 

を試してみましたが、まだ何も動作

public static function readAlllisted($status){ 
       try{ 

        // = pending ORDER BY id DESC' 
        $db = Database::getInstance(); 
        $dbh = $db->getConnection(); 
        $results = $dbh->query('SELECT * FROM application where status = {$status} order by id desc limit 0,30');//select * from data where cat_id=12 order by id desc limit 0,30 
        return $results; 
       }catch(Exception $e){ 
        return $e->getMessage(); 
       } 

1 id Primary int(11) 
2 _customer_id char(8) 
3 personal_id int(11) 
4 emp_id int(11) 
5 fin_id int(11) 
6 status varchar(100) 

私は方法を次のように私のテーブル構造がありますメソッドを呼び出す方法は次のとおりです。>>

あなたが間にそれを置く必要がありますので#statutがvarchar型であるため、
  $app = Application::readAlllisted("pending"); 
       foreach($app as $r){ 
       echo $r['status']; 
       echo '<br/>'; 
       echo $r['personal_id']; 
       echo '<br/>'; 
       echo $r['emp_id']; 
       echo '<br/>'; 

      } 
      ?> 
+0

''* SELECT * FROM application where status = {$ status} order by id desc limit 0,30''は'' '(一重引用符)で囲まれています。 ''二重引用符で囲む – roullie

+0

例外が発生していますが、返された文字列はチェックしません。if(is_array($ app)){foreach(...)} else {echo 'oops、error'。$ app;} ' –

答えて

0

クエリでは、この使用して再度試してみてください「『$のstatut。』」:この要求は、それをデバッグしようとした後

$results = $dbh->query("SELECT * FROM application where status = '".$status."' order by id desc limit 0,30"); 

をし、入力して、結果の内容を参照してください

$app = Application::readAlllisted("pending"); 
print_r($app);//to see if the query is really getting result or not if every thing is okay so you can print the data you want by acceding the associative array 
+0

ありがとうございます。本当にありがとうございます。 –

1

あなたのクエリは正しいですが、ちょうど次のように"に変更し、'でそれを同封しました:

$results = $dbh->query("SELECT * FROM application where status = $status order by id desc limit 0,30"); 

そしてうまくいくはずです。一重引用符を含む文字列はそのまま変数として返されます(文字列)。あなたのケースでは、MySQLで実行されているクエリはステータス値が '$ status'の行を文字通り探しています。

関連する問題