简体   繁体   English

[PHP / MySQL]:循环中的预处理语句

[英][PHP/MySQL]: Prepared Statement in a loop

My problem seems like a rather small one to me, yet, I cannot figure out a proper solution. 我的问题对我来说似乎是一个很小的问题,但是,我找不到合适的解决方案。 The Setup: I have a table 'city_locations' with the columns 'country', 'city', 'longitude', 'latitude'. 设置:我有一个表“ city_locations”,其中有“国家”,“城市”,“经度”,“纬度”列。 The countrys are given by 2-Letter ISO codes. 国家/地区通过2字母ISO代码指定。 I want them to be full names. 我希望他们全名。

For this, I have imported the table 'countrycodes', containing only the columns 'name' and 'code'. 为此,我导入了表“ countrycodes”,其中仅包含列“ name”和“ code”。

$namechange = $con->prepare("UPDATE city_locations SET country=? WHERE country=?");

$list = $con->prepare("SELECT name, code FROM countrycodes");
$list->execute();
$list->bind_result($name, $code);

$namechange->bind_param('ss', $name, $code);

while ($list->fetch()){
    while ($namechange->execute()) {}
    echo "$code is now $name <br>";
}

I succesfully retrieve all pairs in the (outer) while loop. 我成功地检索了(外部)while循环中的所有对。

$namechange->execute(); $ namechange-> execute(); however doesn't do anything - I tried it with and without the while loop, tried using LIMIT 0, 10000 in the query (though I'm not entirely sure I understand LIMIT right). 但是什么也没做-我在有和没有while循环的情况下都尝试过,尝试在查询中使用LIMIT 0,10000(尽管我不确定我对LIMIT的理解是完全正确的)。 With and without the while loop, the statement doesn't do anything. 使用和不使用while循环,该语句都不会执行任何操作。 With LIMIT 0, 10000 the statement cannot be preapred properly (gives an error). 如果使用LIMIT 0、10000,则不能正确地执行该语句(产生错误)。

I also tried to bind the params new in every step of the while loop - didn't seem to do anything either. 我还尝试在while循环的每一步中绑定新的参数-似乎也什么也没做。

When running the same command from my web interface, it works fine. 从我的Web界面运行相同的命令时,它可以正常工作。 However, in that case, I have to type all 200+ codes manually. 但是,在那种情况下,我必须手动键入所有200多个代码。 Seems like a bit much work. 似乎需要做很多工作。

Thanks a lot for your help, Kjeld. 非常感谢您的帮助,Kjeld。

EDIT: $con is of type mysqli. 编辑:$ con是mysqli类型。

$namechange = $con->prepare("UPDATE city_locations SET country=? WHERE country=?");
$list = $con->prepare("SELECT name, code FROM countrycodes");
$list->execute();
$list->bind_result($name, $code);



while ($list->fetch()){
    $namechange->bind_param('ss', $name, $code);
    $namechange->execute();

    echo "$code is now $name <br>";
}

it will probably solve your problem. 它可能会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM