如何在 PHP 中有条件地从数据库中检索行?
好的,我有一个充满值的数据库,其中一个字段值用于潜在客户,另一个字段值用于客户...
我只想检索客户信息...
我该如何编写该函数???
更新
这是我尝试编写的脚本:
<?php
try {
$sql = "SELECT * FROM clients" // WHERE history" or die(mysql_error());
foreach ($dbh->query($sql) as $row) {
$row['history'] = $value;
if ($value == 'clients'){
echo "1212";
} else {
echo "Failed";
return;
}
}
$dbh = null;
} catch (PDOException $e) {
echo "Failed: " . $e->getMessage();
$dbh->rollback();
}
?>
Ok, I have a database full of values with one field value for prospects and another for clients...
I'd like to retrieve only the clients information...
How do I write the function???
UPDATE
Here is the script I tried to write:
<?php
try {
$sql = "SELECT * FROM clients" // WHERE history" or die(mysql_error());
foreach ($dbh->query($sql) as $row) {
$row['history'] = $value;
if ($value == 'clients'){
echo "1212";
} else {
echo "Failed";
return;
}
}
$dbh = null;
} catch (PDOException $e) {
echo "Failed: " . $e->getMessage();
$dbh->rollback();
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有理由在这里进行回滚,特别是因为您还没有开始事务,而这只是一个 SELECT,所以没有什么可以回滚的......我也不确定为什么您要清空 $dbh。可以将 $dbh 重用于其他查询或整个应用程序...
此外,您的 select 语句应该反映您实际需要的数据。如果您需要的只是历史记录,那么 SELECT History FROMclients[...] 是最好的。
There's no reason to do a rollback here, especially since you haven't started a transaction, and this is just a SELECT, so there's nothing to rollback ... I'm also not sure why you're nulling out $dbh. It's possible to reuse $dbh for other queries, or throughout your application...
Also, your select statement should reflect what data you actually need. If all you need is history, then SELECT history FROM clients[...] is best.
根据您的示例脚本,这会执行相同的操作,但它将条件运算符放置在数据库层的查询中,而不是应用程序层的脚本中:
当然,它显然不会像您的示例那样反映非客户端行确实如此,但根据我对你问题的理解,这就是你真正希望发生的事情。
Based on your sample script this would do the same but it would place the conditional operator in the query at the database layer instead of within the script at the application layer:
Of course, it obviously won't reflect non-client rows like your sample did, but from what I could understand of your question this was what you actually wanted to have happen.