我有sql + php查询,我需要在更新失败时告知用户exmpl:
$sql = "UPDATE db SET
date = GetDate(),
...
...
...
WHERE name = '$name1' and code = '$code' and value1 = '$value1' and value2='$value2'
";
sqlsrv_query( $con, $sql);
现在如果php变量值不是100%匹配db update中的值失败但用户无法看到。他可以查看记录然后再试一次。我想在查询更新时通知他。
答案 0 :(得分:1)
与GOB评论一样,您可以使用PHP sqlsrv_rows_affected
函数来检索受影响的行数。例如:
$stmt = sqlsrv_query( $conn, $sql , $params, $options );
$row_count = sqlsrv_rows_affected( $stmt );
if ($row_count === false)
echo "Error in retrieving row count.";
else
echo $row_count;
答案 1 :(得分:1)
在直接执行更新查询之前,请检查更新查询中的条件是否存在。这可以通过选择该条件的计数来完成。
尝试以下代码:
$sql = "select count(*) as count from db WHERE name = '$name1' and code = '$code' and value1 = '$value1' and value2='$value2' ";
while($row = mysqli_fetch_array($sql))
{
$count = $row['count'];
}
if($count == 0)
{
echo 'update will fail';
}
else
{
$sql = "UPDATE db SET
date = GetDate(),
...
...
...
WHERE name = '$name1' and code = '$code' and value1 = '$value1' and value2='$value2'
";
}