循环后bind

编程入门 行业动态 更新时间:2024-10-24 10:19:07
循环后bind_param数组php更新表(bind_param array php update table after a loop)

我有这个代码

$sql="INSERT INTO cerca_strutture (id, struttura, indirizzo, lat, lng, distanza) SELECT id, struttura, indirizzo, lat, lng, P1AP_NOME FROM lista"; $query = $db->query($sql); $lat1="39.3048001"; $lng1="16.2523086"; $sql= "SELECT * FROM cerca_strutture"; $query = $db->query($sql); while($row = $query->fetch_assoc()) { $id=$row['id']; $lat_struttura=$row['lat']; $lng_struttura=$row['lng']; $url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$lat1,$lng1&destinations=$lat_struttura,$lng_struttura&mode=driving&language=it-IT&sensor=false"; $data = @file_get_contents($url); $result = json_decode($data, true); foreach($result['rows'] as $distance) { $distanza=$distance['elements'][0]['distance']['text']; }

我想用这个新值$ distanza更新表cerca_strutture

所以我在这段代码中做了

$sql2 = 'UPDATE lista SET `distanza` = ? WHERE id = ?'; $update = $db->prepare($sql2); $update->bind_param('di', $distance, $id); $update->execute();

但结果是

Fatal error: Call to a member function bind_param() on a non-object in ....

I have this code

$sql="INSERT INTO cerca_strutture (id, struttura, indirizzo, lat, lng, distanza) SELECT id, struttura, indirizzo, lat, lng, P1AP_NOME FROM lista"; $query = $db->query($sql); $lat1="39.3048001"; $lng1="16.2523086"; $sql= "SELECT * FROM cerca_strutture"; $query = $db->query($sql); while($row = $query->fetch_assoc()) { $id=$row['id']; $lat_struttura=$row['lat']; $lng_struttura=$row['lng']; $url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$lat1,$lng1&destinations=$lat_struttura,$lng_struttura&mode=driving&language=it-IT&sensor=false"; $data = @file_get_contents($url); $result = json_decode($data, true); foreach($result['rows'] as $distance) { $distanza=$distance['elements'][0]['distance']['text']; }

I'd like to update the table cerca_strutture with this new value $distanza

So I do in the while this code

$sql2 = 'UPDATE lista SET `distanza` = ? WHERE id = ?'; $update = $db->prepare($sql2); $update->bind_param('di', $distance, $id); $update->execute();

But the result is

Fatal error: Call to a member function bind_param() on a non-object in ....

最满意答案

鉴于您说要更新表,请使用表cerca_strutture。 您需要像这样编写代码

$sql2 = 'UPDATE table cerca_strutture SET distanza = :diz WHERE id = :id'; $update = $db->prepare($sql2); //bind the two parameters separately for better readablity $update->bind_Param(':diz', $distance); $update->bind_Param(':id', $id); $update->execute();

Given that you said you want to update the table,table cerca_strutture. You will need to write your code like this

$sql2 = 'UPDATE table cerca_strutture SET distanza = :diz WHERE id = :id'; $update = $db->prepare($sql2); //bind the two parameters separately for better readablity $update->bind_Param(':diz', $distance); $update->bind_Param(':id', $id); $update->execute();

更多推荐

本文发布于:2023-08-02 00:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1367985.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:bind

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!