gpt4 book ai didi

php - 接收 mysqli_result 中的内容

转载 作者:行者123 更新时间:2023-11-29 08:25:42 25 4
gpt4 key购买 nike

我正在尝试将此代码从 Java 复制到 php 中,但我无法准确弄清楚如何获取行/列的内容?

这是java代码:

ResultSet resultsRS = statement.executeQuery("select distinct snum from shipments where quantity >= 100");

int rowCount=0;

while(resultsRS.next()){
statement2.executeUpdate("UPDATE suppliers SET status = status + 5 WHERE snum = "+"\""+resultsRS.getString(1)+"\"");
rowCount++;
}

到目前为止我在 php 中有这个:

$result2 = mysqli_query($database,"select distinct snum from shipments where quantity >= 100");

$rowCount = 0;

foreach($result2 as $shipment){
mysqli_query($database,"UPDATE suppliers SET status = status + 5 WHERE snum = "."\"".$shipment."\"");
rowCount++;
}

但这并没有按预期工作。

此外,如果您注意到我只想更新一个特定的列。

编辑:让它工作

对于每个阅读我的人来说,我都可以通过以下代码得到我想要的东西:

for($counter=0; $row = mysqli_fetch_row($result2);$counter++){              
foreach($row as $key=> $value){
mysqli_query($database,"UPDATE suppliers SET status = status + 5 WHERE snum = "."\"".$value."\"");
$rowCount++;
}
}

最佳答案

您应该阅读 PHP MYSQLI 文档:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($result = $mysqli->query($query)) {

/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}

/* free result set */
$result->free();
}

/* close connection */
$mysqli->close();
?>

来自 Mysqli fetch

关于php - 接收 mysqli_result 中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18011888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com