Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter:如何在输入前检查DB表中的重复项?_Php_Codeigniter - Fatal编程技术网

Php CodeIgniter:如何在输入前检查DB表中的重复项?

Php CodeIgniter:如何在输入前检查DB表中的重复项?,php,codeigniter,Php,Codeigniter,我正在编写一个小应用程序,将csv文件的内容写入CodeIgniter中的数据库,但在写入之前我很难检查重复。由于这是一项相当常见的任务,我想知道一些专业人士(你们)是如何做到这一点的。为了便于上下文和比较,以下是我的(当前不工作)代码: 除了“你真烂!”(我已经在这一点上每小时从我自己的大脑中听到两次)之外的任何输入都将非常感谢。谢谢。首先您可能想要(而不是>0) 还可以将所有数据推送到一个临时暂存表中,然后用一些漂亮的sql清除该临时暂存表,然后推回到暂存表中 在你使用mysql(如果你是p

我正在编写一个小应用程序,将csv文件的内容写入CodeIgniter中的数据库,但在写入之前我很难检查重复。由于这是一项相当常见的任务,我想知道一些专业人士(你们)是如何做到这一点的。为了便于上下文和比较,以下是我的(当前不工作)代码:

除了“你真烂!”(我已经在这一点上每小时从我自己的大脑中听到两次)之外的任何输入都将非常感谢。谢谢。

首先您可能想要(而不是>0)

还可以将所有数据推送到一个临时暂存表中,然后用一些漂亮的sql清除该临时暂存表,然后推回到暂存表中

在你使用mysql(如果你是phping的话,可以是mysql,也可以是postgres)时,你可以试试这个(摘自) 您必须将.ID更改为主键

delete from salesperson_temptable 
    USING salesperson_temptable, salesperson_temptable as vtable
    WHERE (NOT salesperson_temptable.ID=vtable.ID)
        AND (salesperson_temptable.salesperson_name=vtable.salesperson_name)
然后,当您查看salesperson表的内容时,您可以首先将其插入salesperson表中(而不是>0)

还可以将所有数据推送到一个临时暂存表中,然后用一些漂亮的sql清除该临时暂存表,然后推回到暂存表中

在你使用mysql(如果你是phping的话,可以是mysql,也可以是postgres)时,你可以试试这个(摘自) 您必须将.ID更改为主键

delete from salesperson_temptable 
    USING salesperson_temptable, salesperson_temptable as vtable
    WHERE (NOT salesperson_temptable.ID=vtable.ID)
        AND (salesperson_temptable.salesperson_name=vtable.salesperson_name)

然后,当您查看salesperson的内容时,您可以将其插入salesperson表中

您所做的将起作用,但如上所述,比较应为“==0”而不是“>0”

您还可以使用CodeIgniters内置的num_rows方法,而不是使用sizeof,如下所示:

$sql = "your query";
$query = $this->db->query($sql);

if ($query->num_rows() == 0) {
  // no duplicates found, add new record
}

另外,如果您使用的是CodeIgniter,那么使用Active Record来构建查询而不是编写SQL是值得的。您可以在此处了解更多信息:

您所做的将起作用,但如上所述,比较应为“==0”而不是“>0”

您还可以使用CodeIgniters内置的num_rows方法,而不是使用sizeof,如下所示:

$sql = "your query";
$query = $this->db->query($sql);

if ($query->num_rows() == 0) {
  // no duplicates found, add new record
}
另外,如果您使用的是CodeIgniter,那么使用Active Record来构建查询而不是编写SQL是值得的。您可以在此处了解更多信息: