我试图做这个查询
SELECT category_request.request_id
FROM category_request, event, user
WHERE category_request.category_name = event.category_name
AND user.user_id = event.moderator_id
当我试图在我的数据库上运行时,它的工作正常,如下所示:
但出于某种原因(也许是因为我正在加入?)我得到一个错误:
mysqli_stmt_bind_param():变量数与预准备语句中的参数数量不匹配
我肯定知道只需要表格中的一个列 这是php文件
<?php
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/config.php');
/* $event_name = $_POST["event_name"];
$event_description = $_POST["event_description"];
$event_date = $_POST["event_date"];
$event_time = $_POST["event_time"];
$max_members = $_POST["max_members"];
$ack_needed = $_POST["ack_needed"];
$category_name = $_POST["category_name"];
$event_location = $_POST["event_location"]; */
$event_name = (isset($_POST['event_name']) ? $_POST['event_name'] : '');
$event_description = (isset($_POST['event_description']) ? $_POST['event_description'] : '');
$event_date = (isset($_POST['event_date']) ? $_POST['event_date'] : '');
$event_time = (isset($_POST['event_time']) ? $_POST['event_time'] : '');
$max_members = (isset($_POST['max_members']) ? $_POST['max_members'] : '');
$ack_needed = (isset($_POST['ack_needed']) ? $_POST['ack_needed'] : '');
$category_name = (isset($_POST['category_name']) ? $_POST['category_name'] : '');
$event_location = (isset($_POST['event_location']) ? $_POST['event_location'] : '');
$moderator_id = (isset($_POST['moderator_id']) ? $_POST['moderator_id'] : '');
$result = mysqli_prepare( $con, "SELECT category_request.request_id FROM category_request, event, user
WHERE category_request.category_name = event.category_name AND user.user_id = event.moderator_id")
or die(mysqli_error($con));
mysqli_stmt_bind_param($result,"i", $category_id)
or die(mysqli_error($con));
mysqli_stmt_execute($result)
or die(mysqli_error($con));
/* $statement = mysqli_prepare($con,
"INSERT INTO event (event_name, event_description, event_date
, event_time, max_members, ack_needed,
category_name, moderator_id,event_location) VALUES (?, ?, ?, ? ,?, ?, ?, ?, ?)")
or die(mysqli_error($con));
mysqli_stmt_bind_param($statement, "ssssssisis", $event_name, $event_description, $event_date, $event_time, $max_members, $ack_needed,
, $category_name, $moderator_id,$event_location)
or die(mysqli_error($con));
mysqli_stmt_execute($statement)
or die(mysqli_error($con)); */
$response = array();
$response["success"] = true;
echo json_encode($response);
mysqli_close($con)
or die(mysqli_error($con));
?>
答案 0 :(得分:2)
您正在尝试制作预备语句,而不是仅使用mysqli_query
。如果您确实打算使用预准备语句,就像使用mysqli_stmt_bind_param
函数的占位符一样,那么您需要在预准备语句中包含?
作为占位符。
Buuuut,因为我在查询中的任何地方都没有看到category_id
引用,您可能需要使用mysqli_query
并获取所有结果吗?例如:
$result = mysqli_query( $con, "SELECT category_request.request_id FROM category_request, event, user WHERE category_request.category_name = event.category_name AND user.user_id = event.moderator_id");
if(mysqli_num_rows($result)) {
while($row = mysqli_fetch_row($result)) {
// do something with $row
}
} else {
echo "no results";
}
答案 1 :(得分:2)
在您的查询中:
$result = mysqli_prepare( $con, "SELECT category_request.request_id FROM category_request, event, user
WHERE category_request.category_name = event.category_name AND user.user_id = event.moderator_id") or die(mysqli_error($con));
没有占位符,因此mysqli
在到达binding
时不知道该怎么做。
mysqli_stmt_bind_param($result,"i", $category_id)
or die(mysqli_error($con));
占位符告诉驱动程序将绑定值放在何处。所以拿出来:
mysqli_stmt_bind_param($result,"i", $category_id) or die(mysqli_error($con));
或在您的查询中放置一个占位符?
,以便它可以为您绑定(我在下面对您的表结构进行疯狂的假设,如果这是正确的实现,请更正它们。)
$result = mysqli_prepare( $con, "SELECT category_request.request_id FROM category_request, event, user
WHERE category_request.category_name = event.category_name AND user.user_id = event.moderator_id and category_request.category_id = ?") or die(mysqli_error($con));
有关更多示例,请参阅:http://php.net/manual/en/mysqli.quickstart.prepared-statements.php