如何将多个where子句变量传递给bind_param?

时间:2018-05-06 20:18:39

标签: php mysqli

我正在尝试使用预准备语句进行多查询。 我收到了绑定参数错误的数量:

Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in C:\wamp\www\c\index.php on line 35

第35行是:

$stmt->bind_param('i', $user_id);

以下是总代码: 我尝试了几个例子,但无法使其发挥作用。

示例1:     $stmt->bind_param('i', $id); //从会话输出id 例2:

$stmt->bind_param('ii',$id, $user_id);

还有一些尝试。

$id = $_SESSION['id']; //值来自会话及其完整

$stmt = $mysqli->prepare("SELECT users.*, stores.*, products.* FROM stores, products, users WHERE users.id = ? AND stores.user_id = ? AND products.user_id = ?");
$stmt->bind_param('i', $user_id);
$stmt->execute();
$stmt->bind_result($id, $user_id, $pname);
$stmt->fetch();
$stmt->close();
echo $pname;

当我在phpmyadmin中执行查询时,代码在$ sql上工作正常,如下面的2个查询:

$sql = "SELECT users.*, stores.*, products.*\n"

    . "FROM stores, products, users\n"

    . "WHERE ((users.id) AND (stores.user_id) AND (products.user_id))";

没有php代码

SELECT users.*, stores.*, products.* 
FROM stores, products, users 
WHERE ((users.id) 
AND (stores.user_id) 
AND (products.user_id))

这是最接近的例子: 我有错误:

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in C:\wamp\www\c\index.php on line 37

第37行是:

$stmt->bind_result($user_id, $username, $pname);

$id = $_SESSION['id'];

最近的代码:

$stmt = $mysqli->prepare("SELECT users.*, stores.*, products.* FROM stores, products, users WHERE users.id = stores.user_id AND stores.user_id = products.user_id AND products.user_id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($user_id, $username, $pname);
$stmt->fetch();
$stmt->close();
echo $pname;

2 个答案:

答案 0 :(得分:2)

您准备好的声明包含3个问号/占位符。所以你必须准确绑定3个值。

我认为这应该有效:

$stmt = $mysqli->prepare("SELECT users.*, stores.*, products.* FROM stores, products, users WHERE users.id = ? AND stores.user_id = ? AND products.user_id = ?");
$stmt->bind_param('iii', $user_id, $user_id, $user_id);

请参阅示例:http://php.net/manual/de/mysqli-stmt.bind-param.php

答案 1 :(得分:0)

以下是代码的工作示例,我必须更改:

aic

arima_model = ARIMA(df['mm'],order=(7,1,1))
arima_fit = arima_model.fit()
print(arima_fit.aic)

其余代码如下:和它现在正在工作,我想知道它是否可以安全地在一个实时和动态的网站上使用?

有人可以更正代码。 这是总工作代码

SELECT users.*, stores.*, products.* FROM

我尝试了INNER JOIN和LEFT JOIN(在评论中提到),但无法做到。工作

SELECT users.id, users.username, stores.user_id, stores.name, products.user_id, products.pname FROM

与INNER JOIN相同的查询工作。

    $id = $_SESSION['UserId'];
    $stmt = $mysqli->prepare("SELECT users.id, users.username, stores.user_id, stores.name, products.user_id, products.pname 
     FROM stores, products, users 
     WHERE users.id = ? 
     AND stores.user_id = users.id 
     AND products.user_id = users.id");
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $stmt->bind_result($id, $username, $user_id, $name, $user_id, $pname);
    $stmt->fetch();
    $stmt->close();
    echo $pname;

    echo"<br>" .$name;

    echo "<br>".$username;