dtpyvb1873 2014-09-06 14:39
浏览 27

MySQLi使用另一个准备好的语句中的参数准备语句

I'm trying to switch to prepared statements because of the SQL-injection protection that they offer, but I am currently stuck with this.

How do you make a prepared statement query, using data from another one as params?

What I mean is this: If i have a table with people, and another one with their devices, and I want to list all people with all their devices. How do I do that?

What I want to achive is something like this:

    Alex: Pc, Laptop, Mobile
    Annie: Laptop, Mobile
    Danny: Mobile, Printer

What I have written so far is this:

    $people_query = "SELECT id, name FROM people";
    $device_query = "SELECT device_name FROM devices WHERE owner_id = ?";

    if(!$pplstmt = $mysqli -> prepare($people_query)) die("Couldn't prepare people statement!");
    if(!$devstmt = $mysqli -> prepare($device_query)) die("Couldn't prepare device statement!");

    $pplstmt -> execute();
    $pplstmt -> bind_result($person_id, $person_name);
    while($pplstmt->fetch()){

            echo $person_name . ':';

            $devstmt -> bind_param("i", $person_id);    
            $devstmt -> execute();
            $devstmt -> bind_result($device_name);
            while($devstmt->fetch()){
                echo $device_name . ',';
            }
      }
      $pplstmt -> close();
      $devstmt -> close();

I have moved the prepare statement of the second query out of the loop, because I read that this way it won't initialize again, and again, and again...

This however doesn't work. Any ideas, or advices?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大