如何在php中使用准备好的语句使用选择查询?

问题描述 投票:-2回答:1

我有一个表month,其中只有一列status。状态列仅具有默认值false

我想实现的是当它是每月的第一天时,column(table)中的值应该为true。下次执行代码时,我必须检查数据库中的值是否为true,如果为true,则表示我已经完成了当月的操作,因此第二天就不再重复了,我们将值再次设置为false,以便下个月可以使用。

这是我尝试过的:

php代码

$status=false;
$stmt_check = $connect->prepare("SELECT * FROM month_abc WHERE status =?");
$stmt_check->bind_param("s", $status);
$stmt_check->execute();
print_r($stmt_check);  // Line A
$result=$stmt_check->get_result();
$status_from_db=$result->fetch_object();

if($status_from_db==false) {
    if ((date('j') == 1)) {
        $status=true;
        $stmt= $connect->prepare("UPDATE month_abc SET status=?");   //Line B
        $stmt->bind_param('s', $status);
        $stmt->execute();
        $data->house_sitting_data_yes_no_current_month = array_fill(0, count($data->house_sitting_data_yes_no_current_month) , nada);
    }
} else if($status_from_db==true) {
    if ((date('j')!=1)) {
        $status=false;
        $stmt= $connect->prepare("UPDATE month_abc SET status=?");  // Line C
        $stmt->bind_param('s', $status);
        $stmt->execute();
    }
} 

问题陈述:

在A行,它没有打印任何内容。我想知道上面的php代码中是否有任何错误。 B行和C行会更新表格。

php mysql prepared-statement
1个回答
-2
投票

$ status不是字符串。

将bind_param()更改为整数,true为1,false为0。所以试试这个:

$stmt_check->bind_param('i', $status);
© www.soinside.com 2019 - 2024. All rights reserved.