如何在 PHP 中有条件地从数据库中检索行?

发布于 2024-08-04 20:29:02 字数 679 浏览 7 评论 0原文

好的,我有一个充满值的数据库,其中一个字段值用于潜在客户,另一个字段值用于客户...

我只想检索客户信息...

我该如何编写该函数???

更新

这是我尝试编写的脚本:

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔嚣张 2024-08-11 20:29:02

没有理由在这里进行回滚,特别是因为您还没有开始事务,而这只是一个 SELECT,所以没有什么可以回滚的......我也不确定为什么您要清空 $dbh。可以将 $dbh 重用于其他查询或整个应用程序...

此外,您的 select 语句应该反映您实际需要的数据。如果您需要的只是历史记录,那么 SELECT History FROMclients[...] 是最好的。

<?php 
try { 
    $sql   = "SELECT * FROM clients WHERE history = 'clients'"; 
    $query = $dbh->prepare($sql);

    $query->execute();

    while($row = $query->fetch())
    {
      if($row['history'] == 'clients'){
        echo '1212';
      }
    }
} catch (PDOException $e) { 
    echo "Failed: " . $e->getMessage(); 
}
?>

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.

<?php 
try { 
    $sql   = "SELECT * FROM clients WHERE history = 'clients'"; 
    $query = $dbh->prepare($sql);

    $query->execute();

    while($row = $query->fetch())
    {
      if($row['history'] == 'clients'){
        echo '1212';
      }
    }
} catch (PDOException $e) { 
    echo "Failed: " . $e->getMessage(); 
}
?>
最美的太阳 2024-08-11 20:29:02

根据您的示例脚本,这会执行相同的操作,但它将条件运算符放置在数据库层的查询中,而不是应用程序层的脚本中:

<?php 
    try { 
        $sql = "SELECT * FROM clients WHERE history = 'clients'" // WHERE history" or die(mysql_error()); 

        foreach ($dbh->query($sql) as $row) {                       
            echo "1212";  
        } 

        $dbh = null; 
    } catch (PDOException $e) { 
        echo "Failed: " . $e->getMessage(); 
        $dbh->rollback(); 
    }
?>

当然,它显然不会像您的示例那样反映非客户端行确实如此,但根据我对你问题的理解,这就是你真正希望发生的事情。

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:

<?php 
    try { 
        $sql = "SELECT * FROM clients WHERE history = 'clients'" // WHERE history" or die(mysql_error()); 

        foreach ($dbh->query($sql) as $row) {                       
            echo "1212";  
        } 

        $dbh = null; 
    } catch (PDOException $e) { 
        echo "Failed: " . $e->getMessage(); 
        $dbh->rollback(); 
    }
?>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文