简体   繁体   English

始终会影响MySQL中的行

[英]Always affect Row in MySQL

In PHP, I'm using mysqli_affected_rows to validate whether a user has been registered in a database, where a query looks something like this: 在PHP中,我使用mysqli_affected_rows来验证用户是否已在数据库中注册,查询看起来像这样:

"UPDATE schedule WHERE userid = '$user' AND date='2018-05-05'"

If the user doesn't exist, the number of affected rows is 0, and if they do exist, it's 1. 如果用户不存在,则受影响的行数为0,如果确实存在,则为1。

This works OK, except for when a user is registered in a second time. 除第二次注册用户外,此方法均正常。 Here, MySQL will say 0 rows are affected, since there has been no change to the schedule. 在这里,MySQL会说0行受到影响,因为时间表没有变化

Is there anything I can add to my query to always affect rows? 我可以添加到查询中以始终影响行的任何内容吗? Or is this just a poor approach? 还是这只是一个糟糕的方法?

To check whether a user exists in a database, I would use a select statement instead. 为了检查数据库中是否存在用户,我将使用select语句。

sql = "SELECT * FROM schedule WHERE userid = '$user' AND date='2018-05-05'"

Then do something if a user exists 如果用户存在,则执行某些操作

$result = $conn->query($sql);
if ($result->num_rows > 0) {
//user exists 
}

I would also assume you would like to have unique users in the database? 我还要假设您想在数据库中拥有唯一的用户? I that case I would check whether a user exists first using the same code before adding a new record. 在这种情况下,我将先检查用户是否存在,然后再使用同一代码添加新记录。

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

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