gpt4 book ai didi

php - MYSQL中的选择——选择所有记录

转载 作者:行者123 更新时间:2023-12-01 00:06:52 25 4
gpt4 key购买 nike

我有一个如下的函数:

public function select($id=null) {
$query = "select * from press_releases where id = {$id}";

$results = $this->db->query($query);

return $results;
}

如果在函数调用期间未指定 $id,我想返回所有记录,但我认为 NULL 默认值不是一个好的选择。除了使用未在 where 部分中指定 id 字段的另一个查询之外,我应该怎么做而不是 NULL 来检索所有记录。

谢谢...

最佳答案

$query = "select * from press_releases";
if (!empty($id))
{
$query .= "where id = {$id}";
}

如果 $id 是一个整数,我会使用:

$id = (int) $id;
if ($id > 0)
...

无论哪种方式,我都会确保 $id 可以安全使用。

关于php - MYSQL中的选择——选择所有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6554031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com