dongqiang8474 2013-07-11 09:32
浏览 36
已采纳

mysqli来自更新查询中使用的选择查询

I am having problems with only being able to have one result set from mysqli open at once. In particular I am trying to loop through a select query and update columns within that query after performing an action.

$db = new mysqli($DBServer, $DBUser, $DBPass , $DBName);
$sql = 'SELECT UPRN, POSTCODE FROM T_TEMP';
$stmt = $db->prepare($sql);
$stmt -> Execute();

<Create an array from the above select statement as i understand that mysqli can 
only hold one result set at once (seems odd). I am unsure how to do this such that
i can then reference UPRN and POSTCODE later>

$stmt->Close();

$sql = 'update T_TEMP set LAT = ?, LONG = ? where UPRN = ?';
$stmt = $db ->prepare($sql);  

<loop through that array built above grabbing UPRN and POSTCODE as you go through>

$postcode = urlencode(<Reference the postcode in the array>);
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$postcode."&sensor=false"; 
$xml = simplexml_load_file($request_url);

$lat = round(floatval($xml->result->geometry->location->lat),4);
$long = round(floatval($xml->result->geometry->location->lng),4);

$stmt -> bind_param('ddi',$lat,$long,$UPRN);

$stmt -> Execute();

<end loop>

I am struggling with getting the results of the first query into an array and then referencing that array within the loop so i can set the values. Any help much appreciated!

  • 写回答

2条回答 默认 最新

  • dougua9328 2013-07-11 10:13
    关注

    Don't use mysqli. Use PDO instead. In this latter case this desired code would be of one single line:

    include 'db.php';
    $sql  = 'SELECT UPRN, POSTCODE FROM T_TEMP';
    $stmt = $db->prepare($sql);
    $stmt->execute();
    $array = $stmt->fetchAll(); // here you are
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler