如何使用准备好的语句从 mysql 中获取多行

格拉曼达盛大

使用prepare语句我只获取一行,我尝试while循环所有行,但只有一行女巫被获取。请帮助我如何从数据库中获取所有行而不是一行

PHP 函数: .....

  public function StudentsOfParent($mobile){
 $stmt = $this->conn->prepare("SELECT
                         a.id,
                         a.name, 
                         a.mobile, 
                         c.id as sutdentId, 
                         c.user_id, 
                         c.full_name, 
                         c.school, 
                         c.level,
                         c.year,
                         c.id                    
                         from users a 
                         join students c 
                         on a.id = c.user_id where a.mobile= ?");

    $stmt->bind_param("i", $mobile);
    if ($stmt->execute()) {

   while ($user = $stmt->get_result()->fetch_assoc())
            {
        $stmt->close();

            // return user's results
            return $user;
                }
            }
        else {
        return NULL;
    }
}
  .....

访问上述函数的外部 php 文件:retrieve.php:

  <?php 
  include './DbHandler.php';
   $db = new DbHandler(); 
    // json response array
  $response = array("error" => FALSE);
     if (isset($_POST['mobile'])){
  $mobile = $_POST['mobile']; 
   $user = $db->StudentsOfParent($mobile);
    if ($user != false) {            
      // user found successfully
        $response["error"] = FALSE;
        $response["user"]["id"] = $user["id"];
 $response["user"]["sutdentId"] = $user["sutdentId"];
 $response["user"]["user_id"] = $user["user_id"];
 $response["user"]["full_name"] = $user["full_name"];
 $response["user"]["school"] = $user["school"];
 $response["user"]["level"] = $user["level"];
 $response["user"]["year"] = $user["year"];
        // $response["user"]["photo"] = $user["photo"];
                     echo json_encode($response);
         // $json = json_encode($response);
    }    else {
    // user is not found with the credentials
    $response["error"] = TRUE;
    $response["error_msg"] = "Sorry we could not find you !";
    echo json_encode($response);
   }
    } 
   else {
    // required post params is missing
   $response["error"] = TRUE;
    $response["error_msg"] = "Required parameter is missing!";
   echo json_encode($response);
   }
  ?>
拉杰迪普·保罗

使用准备语句,我只获取一行,我尝试 while 循环所有行,但只获取一行女巫。

那是因为您在循环本身$user的第一次迭代中返回while,循环甚至不会继续进行第二次迭代。另外,您还在$stmt->close();第一次迭代中关闭了语句对象相反,您的代码块应该是这样的:

// your code
if ($stmt->execute()) {
    $result = $stmt->get_result();
    $usersArr = array();
    while ($user = $result->fetch_assoc()){
        $usersArr[] = $user;
    }
    $stmt->close();
    return $usersArr;
} else {
    return NULL;
}
// your code

现在返回的$usersArr数组是一个多维数组,您需要适当地循环遍历以获取所有用户的详细信息。如果要查看完整的数组结构,请执行var_dump($usersArr);.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

NEWID函数导致MYSQL中的准备好的语句出错

如何使用准备好的语句用concat更新MySQL文本列?

MySQL加载数据:在准备好的语句协议中尚不支持此命令

如何使用准备好的语句从MySQL数据库获取日期列并设置其格式

如何使用准备好的语句将UUID插入MYSQL表Binary(16)

如何在MySQL中使用准备好的语句创建动态列?

如何获取准备好的语句中的批次数?

Sqlx使用准备好的语句获取

如何在Sequelize中创建准备好的语句?

使用准备好的对象语句无法从mysql获取多行数据

如果列之一是使用准备好的语句自动递增,如何在mysql数据库中插入值

使用准备好的语句在MySql中插入(DATE(now))

将准备好的语句的结果存储为mysql中的表?

mysql准备好的语句我应该使用哪条语句

在使用准备好的语句进行查询的过程中,如何使用PHP填充数组

如何使mysqli准备好的语句并获取结果?

java mysql准备好的语句

我如何使用准备好的语句更新mysql数据库中的值

在mysql中获取准备好的语句列表

如何使用准备好的语句在MySQL中插入多行而不插入空白行

使用准备好的语句写入 mysql

MySQL准备好的语句在PHP中抛出错误

在 mysqli 中使用准备好的语句时获取数据

MySQL Select 查询在终端中工作。在 php 准备好的语句中失败

使用准备好的语句 mysqli 获取查询结果

如何将 mysql 5.7 中 json 列中的动态键数更新为准备好的语句

使用 php 和准备好的语句写入 mysql

如何在 MySQL 准备好的语句中包含“ ”?

是否可以(通过 PHP)使用先前在 mysql CLI 中声明的准备好的语句?