【问题标题】:pre-pared statement query not returning any results预处理语句查询不返回任何结果
【发布时间】:2014-01-21 23:10:42
【问题描述】:

我需要将一些数据库行 ID 发送到另一个页面,然后使用它们的 ID 来加载行和处理。我使用了一个工作正常的会话变量。

$_SESSION['tmp'] = "50,51,52";

    if ($stmt = $mysqli->prepare("SELECT id,jpg WHERE id = ?")) {
        $stmt->bind_param("s",$_SESSION['tmp']);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($result_id,$result_jpg);
        if ($stmt->num_rows > 0) {
            while($stmt->fetch()) {
                $image = array($result_id => $result_jpg);
                print_r($image."<br />");
            }
        } else {
            echo "Prepare failed: (" . $stmt->errno . ") " . $stmt->error;
        }
    }

查询将是

选择 id,jpg WHERE id = 50,51,52

它应该返回所有的行,但是根本没有显示任何内容,没有错误或任何东西。有任何想法吗?

######编辑#####

更新了我的代码:

//Generate the query
    $query = "SELECT id,jpg FROM images WHERE id IN (?";
    for ($i=1; count($_SESSION['tmp']) > $i; $i++) {
        $query = $query.",?"; 
    } $query = $query.")";

    if ($stmt = $mysqli->prepare($query)) {
        for ($i=1; count($_SESSION['tmp']) > $i; $i++) {
            $b = $_SESSION['tmp'][$i]-1;
            $stmt->bind_param("s",$b);
        }
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($result_id,$result_jpg);
        if ($stmt->num_rows > 0) {
            while($stmt->fetch()) {
                $image = array($result_id => $result_jpg);
                print_r($image);
            }
        } else {
            echo "Prepare failed: (" . $stmt->errno . ") " . $stmt->error;
        }
    }

似乎无法循环

bind_param("s",50);

得到错误:

警告:mysqli_stmt::bind_param(): 变量数不匹配 准备好的语句中的参数数量 C:\xampp\htdocs\ppa\add_sales.php 在第 39 行

#####编辑2#####

改变了我做这件事的方式,这很好用。

$image = array();
    foreach($_SESSION['tmp'] as $x) {   
        if ($stmt = $mysqli->prepare("SELECT id,jpg FROM images WHERE id = ?")) {
            $stmt->bind_param("s",$x);
            $stmt->execute();
            $stmt->store_result();
            $stmt->bind_result($result_id,$result_jpg);
            if ($stmt->num_rows > 0) {
                while($stmt->fetch()) {
                    $image[$result_id] = $result_jpg;
                }
            } else {
                echo "Prepare failed: (" . $stmt->errno . ") " . $stmt->error;
            }
        }
    }
    print_r($image);

【问题讨论】:

  • 您尚未指定从哪个表中选择。
  • 准备好了,没有准备好。

标签: php mysql sql


【解决方案1】:
  1. 您错过了FROM 子句
  2. 如果您检查过$mysqli-&gt;error,您自己就知道了
  3. WHERE id = 50,51,52 不是有效的 mysql 语法
  4. 您需要改用IN 运算符
  5. 使用准备好的语句,它将被评估为WHERE id = '50,51,52'。就是这样 - 将 id 列与单个字符串进行比较。

【讨论】:

  • 不敢相信我刚刚做到了,我怎么会错过 FROM...谢谢!
  • @Martyn Lee Ball:FROM 是最少的问题,注意其他点
【解决方案2】:
SELECT id,jpg WHERE id = 50,51,52

这是错误的说法。

1) 你不能在这样的地方写参数。 使用where id = 50 or id = 51 or id = 52

2) 你错过了 FROM:FROM Table

这是正确的:

SELECT id,jpg FROM table WHERE id = 50 or id = 51 or id = 52

【讨论】:

  • 废话!我怎么错过了!谢谢
  • @Sharikov Vladislav:同时提供等于5051 的任意数字。
  • @simult 但您对IN 子句是正确的。在这种情况下会更好。
【解决方案3】:

您可以尝试以下方法: SELECT id,jpg FROM tablename WHERE id IN (50,51,52);

【讨论】:

    【解决方案4】:

    你需要分别绑定它们:

    WHERE id IN (?, ?, ?)
    

    并绑定:

    $stmt->bind_param("i", 50);
    $stmt->bind_param("i", 51);
    $stmt->bind_param("i", 52);
    

    您将需要使用explode() 和一个循环来正确执行此操作。您还需要使用 implode 生成正确数量的问号:

    $questionMarks = '(' . implode(',', array_fill(0, count($resultOfSplit), '?')) . ')';
    

    【讨论】:

    • 我不要$stmt-&gt;bind_param("iii",50,51,52);
    • @Martyn Lee Ball:这取决于您是否有静态数量的参数
    • 你可以,但是如果你的数字参数是可变的(即你从explode()的结果中得到它),那么你不能。
    • @Fabien Warniez:还值得一提的是,对于动态数量的参数,您还需要相应地生成查询
    • $questionMarks 字符串将直接连接到查询,而不是通过 PDO 绑定。 IE。 $query . $questionMarks
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    相关资源
    最近更新 更多