记录计数为零时重定向页面

时间:2016-10-25 21:39:24

标签: php mysql redirect record-count

我有一个运行的脚本,然后指向一个页面来删除一条记录。我想设置它,所以当记录计数为零时,页面重定向到index.php页面。代码如下:

$sql="SELECT * FROM fbi_with_invoice";
if ($result=mysqli_query($conn,$sql))
{
    $rowcount=mysqli_num_rows($result);
    printf("There are %d rows remaining.\n",$rowcount);
}
if ($rowcount=0)   {
    header('refresh:5;url=index.php'); 
    mysqli_free_result($result);
}

$sql = "DELETE FROM fbi_with_invoice ORDER BY personid DESC LIMIT 1";

if ($conn->query($sql) === TRUE) {  
    header('refresh:5;url=index2.php');
}
$conn->close();

目前$rowcount给出0,但页面重定向到index2.php。

1 个答案:

答案 0 :(得分:1)

问题在于:if ($rowcount = 0)您使用了赋值运算符而不是==比较运算符。

     $sql="SELECT * FROM fbi_with_invoice";
    if ($result=mysqli_query($conn,$sql))
    {
    $rowcount=mysqli_num_rows($result);
    printf("There are %d rows remaining.\n",$rowcount);
    }
    if ($rowcount == 0)   {
    header('refresh:5;url=index.php'); 
    mysqli_free_result($result);
    }
    $sql = "DELETE FROM fbi_with_invoice";

    if ($conn->query($sql) === TRUE && $rowcount) {  
    header('refresh:5;url=index2.php');
    }
    $conn->close();