douchen5971 2014-07-09 14:39
浏览 46
已采纳

将MySQLi中的预准备语句作为函数参数传递

Here I want to pass the prepared statements as arguments of test function which should display the query results.But the code below doesn't work,because it may lack something that I don't know.What should I do in order to display results by the test function:

<?php
  $mysqli = new mysqli("localhost","root","","test");

  /*check connection*/
  if(mysqli_connect_errno())
  {
    printf("connection failed: %s
",mysqli_connect_error());
    exit();
  }

  /*create prapared statement*/

  $stmt1 = $mysqli->prepare("select id from posts");
  $stmt2 =$mysqli->prepare("select username from members where id=?");

  $stmt1->execute(); 

  $stmt1->store_result(); 

  $stmt1->bind_result($ID);

  /*bind params*/
  $stmt2->bind_param('i',$id);

  /*bind results*/
  $stmt2->bind_result($username);


   while($stmt1->fetch())
   {    
      /*set params*/
      $id =$ID;

      /*execute prapared statement*/
      $stmt2->execute();$stmt2->store_result();                 

   }

  test($stmt1,$stmt2);//function call  

  function test($stmt1,$stmt2)
  {

   while($stmt1->fetch())
   {     
      while($stmt2->fetch())
      {
        echo 'Username: '.$username.'<br/>';
      }     

   }     

  }

?>
  • 写回答

1条回答 默认 最新

  • doutao5419 2014-07-09 16:07
    关注

    I think this way could be not the lightweight solution but you can reset the result pointer with the mysqli_result::data_seek() method and as you need again the result variable you have to rebind it:

    function test($stmt1, $stmt2)
    {
        $stmt1->data_seek(0);
    
        while($stmt1->fetch())
        {
            $stmt2->data_seek(0);
            $stmt2->bind_result($username);
    
            while($stmt2->fetch())
            {
                echo 'Username: '.$username.'<br/>';
            }     
        }   
    }
    
    test($stmt1, $stmt2);
    

    An option could be to store in an array the binded results inside the while, and then use this array subsequently as many times as you want; However everything depends on your needs.

    Source: http://www.php.net/manual/it/mysqli-result.data-seek.php

    EDIT -------------------

    I just suggested using an array to avoid reusing resource only for looping the same data.

    Anyway, it may depends on your env but in case you have mysqlnd driver installed, you can do something like this:

    $res1 = $stmt1->get_result(); // here you can use get_result() to get the resultset
    
    $myAy = array();
    
    while($row = $res1->fetch_array())
    {
        $myAy[] = $row;
    }
    
    var_dump($myAy);
    

    or another way could be something like this:

    ...
    
    $myAy = array();
    
    while ($stmt1->fetch()) 
    {
        $id = $ID;
        $myAy['id'] = $ID;
    
        ...
    }
    
    while ($stmt2->fetch()) 
    {
        $myAy['user'] = $username;
    }
    
    var_dump($myAy);
    

    Just my two cents...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置