简体   繁体   English

检查记录是否存在

[英]Check if Record Exists

I found this on php.net to check if file exists. 在php.net上发现了这个以检查文件是否存在。

I want to check if record exists first, then do update, otherwise do an insert. 我想首先检查记录是否存在,然后进行更新,否则进行插入。

if(record exist) {
   update query}
else 
  { insert query}

I know how to update or insert, but don't know how to check if a record exists first. 我知道如何更新或插入,但不知道如何检查记录是否存在。 How is it done? 怎么做?

If you know how to do a SQL SELECT , then do that: 如果您知道如何执行SQL SELECT ,那么执行以下操作:

$result = mysql_query("SELECT * FROM table1 WHERE something");
$num_rows = mysql_num_rows($result);

if ($num_rows > 0) {
  // do something
}
else {
  // do something else
}

Better yet, don't do this in PHP, use INSERT ... ON DUPLICATE KEY UPDATE . 更好的是,不要在PHP中这样做,使用INSERT ... ON DUPLICATE KEY UPDATE

You don't need to do this in PHP, you can do it directly in the SQL query for your database (I'm assuming you're using a database for the records, since that's what it sounds like from your question, and I'll assume MySQL since that's often used along with PHP). 您不需要在PHP中执行此操作,您可以直接在数据库的SQL查询中执行此操作(我假设您正在使用数据库进行记录,因为这就是您的问题所听到的内容,而我我会假设MySQL,因为它经常与PHP一起使用)。

INSERT INTO ... ON DUPLICATE KEY UPDATE;

This would insert a new row if it doesn't already exist, or update the current one if it does, assuming your tables have primary keys. 如果新行尚不存在,则会插入新行,如果存在,则会更新当前行,假设您的表具有主键。

See the MySQL Manual for more info on this. 有关详细信息,请参阅MySQL手册

The other option is to just do a SELECT COUNT(1) FROM myTable WHERE ... query first, and then only do an insert if the result is 0, otherwise do an update. 另一种选择是首先执行SELECT COUNT(1) FROM myTable WHERE ...查询,然后仅在结果为0时执行插入,否则执行更新。

你也可以试试这个

INSERT ... ON DUPLICATE KEY UPDATE

I would recommend Dominic Rodger's solution, but with a little change to make it faster. 我会推荐Dominic Rodger的解决方案,但稍加改动就能让它变得更快。 You should select a single value and not more than one row. 您应该选择一个值而不是一行。

$result = mysql_query("SELECT key FROM table1 WHERE something LIMIT 1");
$num_rows = mysql_num_rows($result);

if ($num_rows > 0) {
  // do something
}
else {
  // do something else
}

If your record already exists, you'll get a result, wich is more than 0 results, so it works, but potentially with less traffic from your SQL-Server. 如果您的记录已经存在,您将得到一个结果,结果超过0,因此它可以正常工作,但可能会减少来自SQL-Server的流量。

$username = $_POST["user"];

$query = mysql_query("SELECT * FROM users WHERE username='$username'");

if(mysql_num_rows($query) != 0)
{
echo "Username already exists";
}
else
{
//proceed with code here
}

Count records matching your criteria? 计算符合条件的记录?

select count(*) from foo where id = 5

if($count > 0) {
    // record exists
    ...
}

奇怪没有人提到过使用REPLACE?

I've had the same problem and I solved using REPLACE INTO, a MySQL extension working this way: 我遇到了同样的问题,我解决了使用REPLACE INTO,这是一个以这种方式工作的MySQL扩展:

  • If the record which you want to insert does not exist, the MySQL REPLACE inserts a new record. 如果您要插入的记录不存在,则MySQL REPLACE会插入新记录。
  • If the record which you want to insert already exists, MySQL REPLACE deletes the old record first and then insert a new record . 如果您要插入的记录已存在,则MySQL REPLACE首先删除旧记录,然后插入新记录

I found this method useful when I don't need to remember the old values of the record. 当我不需要记住记录的旧值时,我发现这个方法很有用。 For example, if you want to increase a value this method isn't a great way 'cause delete the old record and the old values as well. 例如,如果要增加值,则此方法不是一种很好的方法,因为还要删除旧记录和旧值。

Remember also you need to have both INSERT and DELETE privileges to use REPLACE. 请记住,您还需要具有INSERT和DELETE权限才能使用REPLACE。

Here's and example based on my code: 这是基于我的代码的示例:

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "REPLACE INTO table_name (name, age, current_date)
       VALUES ('" . $name . "', $age, '" . date('Y-m-d') . "')";

if ($conn->query($sql) === FALSE) {
   echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

You could do a select for that record and inspect the result, or you could try an update and see if there was an error. 您可以对该记录执行选择并检查结果,或者您可以尝试更新并查看是否存在错误。 If there was, then there was nothing to update, so do an insert. 如果有,那么没有更新,所以插入。

You can set the specific columns in your database as primary key and then the insert will success only if you don't have the record already. 您可以将数据库中的特定列设置为主键,然后只有在您没有记录时插入才会成功。 In this way, you don't event need to check if record exists. 这样,您不需要检查记录是否存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM