循环后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();更多推荐
发布评论