我遇到的问题是只能从mysqli一次打开一个结果集。 特别是我尝试循环选择查询并在执行操作后更新该查询中的列。
$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>
我正在努力将第一个查询的结果导入数组,然后在循环中引用该数组,以便我可以设置值。 任何帮助非常感谢!
答案 0 :(得分:1)
不要使用mysqli。请改用PDO。在后一种情况下,这个所需的代码将是一行:
include 'db.php';
$sql = 'SELECT UPRN, POSTCODE FROM T_TEMP';
$stmt = $db->prepare($sql);
$stmt->execute();
$array = $stmt->fetchAll(); // here you are
答案 1 :(得分:-1)
在向正确方向转向后,我结束了将结果集读入数组,然后循环遍历该数组。 (此评论已经被投票,但我正在编辑它以便其他人更容易找到。)
您不能打开两个mysqli查询,因为服务器只允许您在内存中保留一个(实际上在服务器上)。所以你将第一个查询读入php数组,然后循环执行该数组中的mysqli语句。
$vInteger =1; //This is just an example variable
$sql ='select column from table where column = ?'; //select with a passed value
$stmt = db->prepare($sql);
$stmt-> bind_param('i', $vInteger); //again, this could be a string, double or integer
$stmt->execute();
$result = $stmt->get_result(); //retrieves the results from the server into a class
while ($row = $result->fetch_assoc(){
$Array[] = $row; //reads the rows into a new array
}
$stmt -> free_result(); //Drop the mysqli bit cause you are going to need it again! :)
foreach($Array as $item){
$item['Column']; // this is the column name from your original query
// Of course in here you might want to do another mysqli query with the prepare etc etc and reference the $item.
}
如果你决定使用对我有用的mysqli,我认为get_result会创建一个mysqli结果类,你可以应用fetch_assoc方法..