php - 在准备好的语句中绑定(bind)参数

标签 php mysql

今天早些时候我发现我在使用准备好的语句方面已经落后了。我试图制作一个准备好的语句以从我的数据库中获取一些数据。

我想打印数据库中的所有行,但我不太确定如何在 while 循环中执行此操作?

<?php

    /* Prepare */
    if ($stmt = $mysqli->prepare("SELECT * FROM stores")) {
        echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }

    /* Bind and execute */
    $id = null;
    $headline = null;
    $description = null;
    $place = null;
    if (!$stmt->bind_param("i", $id, "s", $headline, "s", $description, "s", $place)) {
        echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
    }

    if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    }

    while ($stmt->fetch()) {
        /* Loop through my rows in MySQL and print all rows*/
        echo $id, $headline, $description,$place;
    }

    /* Close Statement */
    $stmt->close();

    /* Close Connection */
    $mysqli->close();

?>

最佳答案

if (!$stmt->bind_param("isss", $id, $headline, $description, $place))
{
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}

你会想这么做的。正如@Fred-ii 在评论中所说。你的语法错了。

它的工作原理是,bind_param 的第一个参数是将所有数据类型作为一个字符串,然后列出您的数据。确保使用正确的数据类型和正确数量的参数。

更新

进一步检查您的代码后,我意识到您没有正确使用prepare。我将在下面提供一个演示,以便您可以将其用作指南。

$stmt = $mysqli->prepare("SELECT * FROM myTable WHERE id = ? AND name = ?");
if (!$stmt->bind_param("is", $id, $name))
{
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute())
{
    echo "Execution failed: (" . $stmt->errno . ") " . $stmt->error;
}

问号描绘了每个变量。这意味着您可以将 ? 放在您想要变量的位置。
然后,您使用 bind_param 列出您的数据类型(如上所述)以及后面的变量或数据。

更新2

$errors = array(); // store errors here
$stmt = $mysqli->prepare("SELECT name FROM myTable WHERE id = ?"); // prepare our statement

// check that our parameters match, if not then add error
if (!$stmt->bind_param("i", $id))
{
    array_push($errors, "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
}

// if no errors and statement fails to run
if (count($errors) <= 0 && !$stmt->execute())
{
    array_push($errors, "Execution failed: (" . $stmt->errno . ") " . $stmt->error);
}

// no statement errors
if (count($errors) <= 0)
{
    $stmt->bind_result($name); // store the results of the statement in this variable

    // iterate through each row of the database
    while ($stmt->fetch())
    {
        echo $name;
    }
}
// report the errors
else
{
    echo "<h3>Errors</h3>";
    foreach ($errors as $error)
    {
        echo "<p>$error</p>";
    }
}
  • $errors = array()
    在这里,我创建了一个数组来保存所有错误消息。
  • array_push($errors, "...")
    array_push function将以 array_push($array, $var) 的语法向数组添加一个变量,其中 $array 是要添加项目的数组,$ var 是您要添加的项目。
    我使用它是为了能够以一种简洁的方式添加错误,以便稍后进行迭代。
  • 计数($错误)
    count function将计算数组中元素的数量。
    我用它来查看数组中是否添加了任何错误。当我初始化 $errors 时,其中没有元素,因此它将返回 0。
  • $stmt->bind_result($name)
    这是写在 while 循环 之外的,因为它用于告诉语句我们要将所有列 name 存储在名为 $name< 的变量中
  • while ($stmt->fetch())
    这将迭代数据库中的每一行。 while 循环 的每次迭代都将是数据库的一行。在我的示例中,我只是回显 name 列的值。
    可以存储多于一列。只需在 SQL 查询中添加列 (SELECT col1, col2, col3 FROM mytable),然后将每列存储在 bind_result 中的变量中 ($stmt-> bind_result($col1, $col2, $col3);。请注意,它们不必与列同名;这也是有效的 $stmt->bind_result($myVar, $someVar , $anotherVar);)。
  • foreach($errors as $error)
    foreach获取一个数组并迭代它,将每次迭代存储在 as 后面的变量中。
    在本例中,我们将错误存储在名为 $errors 的数组中,并将每个错误存储在 $error 中并将其写入段落标记中。

关于php - 在准备好的语句中绑定(bind)参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43357083/

相关文章:

c# - 如何使用C#在mysql数据库中存储图像路径

PHP cURL,提取 XML 响应

php - 日期之间的错误搜索

从表单值生成 php mysql 更新语句

php - 跟踪登台数据库中的更改然后将接受的数据推送到主数据库的好方法是什么?

mysql - 使用运算符IN的Golang MongoDB更新查询

php - unserialize() ...函数 spl_autoload_call() 尚未定义调用它的类

javascript - 使用 onchange 获取选择的值并添加行 ID 号

mysql - 连接一条记录中的特定行并从第一个表中检索所有数据

mysql - 更新某些行时忽略唯一键(mariaDB)