我有一个问题,我在Stack溢出网站上查找并找到了很多解决方案, 但是,当我尝试使用我的代码时,它似乎不起作用。我希望有人可以帮助找出我的代码有什么问题。
堆栈溢出的一些解决方案
mysql query with where and order by take long time
我正在尝试从数据库中选择status = $ status的记录,但响应始终如下:
警告:第98行的/Users/khloudamer/Documents/Websites/BusinessDoorFinal2/index.php中为foreach()提供的参数无效
方法如下:
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();
}
我试过了
(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
仍然无效
我的表格结构如下
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)
我调用该方法的方式如下>>
$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 :(得分:1)
您的查询是正确的,但是您已将其与'
括起来,只需将其更改为"
,如下所示:
$results = $dbh->query("SELECT * FROM application where status = $status order by id desc limit 0,30");
它应该可以正常工作。因为带有单引号的字符串是返回的,因为它是包含变量的字符串。在你的情况下,在MySQL上运行的查询正在寻找状态值为'$ status'的行。
答案 1 :(得分:0)
因为#statut是一个varchar,所以你应该把它放在'。$。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