duanhu7390 2016-08-20 20:50
浏览 85
已采纳

使用mysqli :: store_result()后获取数据

I am selecting data from my database in the following way: As expected I get one row as result and want to extract the lat and lon data from it:

    $prep_stmt = "SELECT lat, lon FROM members WHERE id = ? LIMIT 1";   
    $stmt = $mysqli->prepare($prep_stmt);

      if ($stmt) {
        $stmt->bind_param('i', $user_id);
        $stmt->execute();
        $result = $stmt->store_result();

        if ($stmt->num_rows == 1) {
          logC("user exists");
          logC("extract lat, lon!");
        } 
      }

I tried using $stmt->fetch() and extracting it from the $result variable but without success. What is the proper way to do it?

  • 写回答

2条回答 默认 最新

  • dqyhj2014 2016-08-21 00:46
    关注

    Since you're expecting just one result:

      if ($stmt = $mysqli->prepare($prep_stmt)) {
        $stmt->bind_param('i', $user_id);
        $stmt->execute();
        $stmt->bind_result($lat, $lon);
    
        if(($result = $stmt->fetch()) == true){
          echo "'$lat' and '$lon'";
        } elseif($result == false) {
          echo 'db error';
        } else {
          echo 'no results'; // $result == null
        }
    
    
      }
    

    If you have multiple rows, $stmt->fetch() will bind the next result to the same variables so you can do:

    while($stmt->fetch()){
      echo $lat, $lon;
    }
    

    It is just 1 way of many.. just read the documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器