我怎么知道我的mysql_query更新是否实际上没有更新任何内容?

Liz*_*zza 0 php mysql insert sql-update

我正在更新一个表可能不存在的行.如果它不存在,我需要插入.我最初计划知道我需要根据update语句中的负返回值进行插入,但它不返回任何负数.

我怎么知道更新没有做任何事情?做另一个查询,看看有什么吗?或者之前可能是查询?

谢谢!

j08*_*691 5

使用http://php.net/manual/en/function.mysql-affected-rows.php

通过与link_identifier关联的最后一个INSERT,UPDATE,REPLACE或DELETE查询获取受影响的行数.

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

/* this should return the correct numbers of deleted records */
mysql_query('DELETE FROM mytable WHERE id < 10');
printf("Records deleted: %d\n", mysql_affected_rows());

/* with a where clause that is never true, it should return 0 */
mysql_query('DELETE FROM mytable WHERE 0');
printf("Records deleted: %d\n", mysql_affected_rows());
?>
Run Code Online (Sandbox Code Playgroud)

如果您使用的mysqlimysql(很久以前mysql已经弃用),那就是.mysqli_affected_rows