【问题标题】:How to put multiple values in WHERE row = (?) SQL php statement [duplicate]如何在WHERE行=(?)SQL php语句中放置多个值[重复]
【发布时间】:2016-03-07 10:45:41
【问题描述】:

所以当我手动输入一些值但在php 中我遇到了一些困难时,我的查询可以在实际的phpmysql 服务器上运行。

这是我的SQL 表:

userID | forename | surname  |      email         | age    | 
------------------------------------------------------------
    1  |  Jack    |  Wolf    |   dj@rave.com      |  19    | 
    2  |  Mark    |  Smith   |   mark@rave.com    |  18    | 
    3  |  Ben     |  Cas     |   sex@club.com     |  21    | 
    4  |  Jos     |  Jis     |   jis@jos.com      |  19    | 
    5  |  Luke    |  Kils    |  kils@kiss.com     |  23    | 
------------------------------------------------------------

基本上,我想传入一些UserID 值,例如1,3,5,它应该显示:

userID | forename | surname  |      email         | age    | 
------------------------------------------------------------
    1  |  Jack    |  Wolf    |   dj@rave.com      |  19    | 
    3  |  Ben     |  Cas     |   sex@club.com     |  21    | 
    5  |  Luke    |  Kils    |  kils@kiss.com     |  23    | 
------------------------------------------------------------

用户 ID 值可能因用户选择而异,因此它可以是 2 甚至是 1,2,3,4 甚至是 1,2,3,4,5

这是我的php 代码:

<?php
require "init.php";
if(!empty($_POST['userID'])){
    $userID = $_POST['userID']; 
    echo $_POST['userID'];  
    $stmt = "SELECT userID, forename, surname, email, age
            FROM users
            WHERE userID IN (?)";   
    $result = $conn-> prepare($stmt);
    $result->bind_param('i', $userID);
    $result->execute(); 
    $outcome=$result->get_result();
    $response = array();
    if(($outcome->num_rows)>0){
        while($row = $outcome->fetch_assoc()){
            $response[] = array
            (
                "userID" => $row["userID"],
                "forename" => $row["forename"],
                "surname" => $row["surname"],
                "email" => $row["email"],
                "age" => $row["age"]
            );
        }
    echo json_encode($response); 
    }
    else{
        echo json_encode("None found");
    }
}

?>

当我回显$userID = $_POST['userID']; 时,我得到1,2,31,但是这些都没有正确传递给SELECT STATEMENT。我该如何解决?

谢谢

【问题讨论】:

  • 简而言之,您不能将单个准备好的参数与in 和可变数量的数组成员一起使用。
  • @apokryfos 那么我该怎么做呢?对不起,我是php的新手

标签: php mysql sql database


【解决方案1】:

类似的东西

...
 $userIDs = implode(",",$_POST['userID']); //Array of user ids
 $qs = array_fill(0,count($userIds),"?");
 $stmt = "SELECT userID, forename, surname, email, age
        FROM users
        WHERE userID IN (".implode(",",$qs).")";   
 $bindParams = $userIDs; //Array for the bind parameters
 $bindParams = array_unshift($bindParams, "i"); //Prefix with "i" 
 call_user_func_array([$result, 'bind_param'],$bindParams);
 ...

这个想法是您的陈述将需要尽可能多的“?”因为你绑定了这么多整数,所以有用户 ID。

您将使用call_user_func_array 调用带有可变数量参数的bind_param 函数。请注意,如果您使用的是 PHP 5.6+,则可以执行以下操作:$result-&gt;bind_param("i",...$userIDs)

【讨论】:

    【解决方案2】:
    select userName from Table where user_id in (1, 2, 3);. 
    

    你可以使用这种格式的 $_POST['userID'] 或者转换这种格式并使用它。

    【讨论】:

    • 我如何转换$_POST['userID'] 来准确地做到这一点?就像我在我的服务器上运行你的sql 语句一样,它可以工作,因为我手动输入了1,2,3。这就是我如何对$_POST['userID'] 做同样的事情
    猜你喜欢
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2019-01-10
    相关资源
    最近更新 更多