我正在尝试使用mysqli获取在我的数据库表中找到的int
。然后我使用bind_result()
绑定结果。但是,当我尝试使用该值时,我只得到一个0。
我怎么处理这个?
我的代码看起来像这样:
$sql = <<<EOF
SELECT
project_salary_amount
FROM projects_set_salary
WHERE project_id = ? ORDER BY project_salary_id DESC LIMIT 1
EOF;
$stmt = $mysqli->prepare($sql) or die ("Feil i database<br>" . $sql . "<br><b>Feilmelding:</b> " . $mysqli->error);
$stmt->bind_param("i", $project_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($dbb_salary_amount);
$db_salary_amount = $dbb_salary_amount;
$num_salary_results = $stmt->num_rows;
$stmt->free_result();
$stmt->close();
if($num_salary_results == 0){
$sql = <<<EOF
INSERT INTO
projects_set_salary (project_id)
VALUES ($project_id)
EOF;
$stmt = $mysqli->prepare($sql) or die ("Feil i database<br>" . $sql . "<br><b>Feilmelding:</b> " . $mysqli->error);
$stmt->execute();
$stmt->close();
$db_salary_amount = 10;
$html_set_salary = "";
$html_set_salary .= "<form id=\"form_send_salary\"method=\"post\" action=\"create_set_salary.php\">\n";
$html_set_salary .= "<input type=\"number\" name=\"set_salary\" value=\"$db_salary_amount\">";
}
else{
$html_set_salary = "";
$html_set_salary .= "<form id=\"form_send_salary\"method=\"post\" action=\"create_set_salary.php\">\n";
$html_set_salary .= "<input type=\"number\" name=\"set_salary\" value=\"$db_salary_amount\">";
}
现在,它不会在输入框中显示10。这意味着它不执行if语句。但是,为什么我没有从$db_salary_amount
获得任何结果?我知道我可能不必将变量从$db_salary_amount
更改为$db_salary_amount
。我只是想找出问题所在。
答案 0 :(得分:3)
在->fetch()
bind_result()
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($dbb_salary_amount);
$stmt->fetch(); // <--- missing this ->fetch()
$db_salary_amount = $dbb_salary_amount;
来自->bind_result()
的文档 - &gt; When mysqli_stmt_fetch() is called to fetch data, the MySQL client/server protocol places the data for the bound columns into the specified variables var1, ....