gpt4 book ai didi

php - while 循环中的 mysql_fetch_assoc 问题 - foreach 循环中的查询格式错误

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

我有 foreach 循环,在 $code 中我得到如下字符串编号:1234 4211 4223 2252 等,尽管使用explode 或 preg_split。

这是代码

foreach ($rowData as $row => $tr) {

foreach ($tr as $td)

$tdm = explode("/\s/", $td);
$code = $tdm[0];

}


$sql_athl="SELECT * FROM `grobar_IDs` WHERE `id`='$code'";
$results=mysql_query($sql_athl);
while($athl_info = mysql_fetch_assoc($results)){

echo $athl_info['NAT'];
}

正如你所看到的,我想打印 mysql 记录,其中空格之间的每个数字都是一个 ID。我猜问题出在 $code 因为 echo $sql_athl; 只给我

SELECT * FROM grobar_IDs WHERE fiscode =' 5451 4321 5142 5497 5253 6098 4322 '

而不是

SELECT * FROM grobar_IDs WHERE fiscode ='5451' SELECT * FROM grobar_IDs WHERE fiscode ='4321'

大家有什么想法或解决方案吗?我被困住了...

最佳答案

首先将您的 code 变量设置为数组。

$code = [];
foreach ($rowData as $row => $tr) {
foreach ($tr as $td) {
$tdm = explode("/\s/", $td);
$code[] = $tdm[0];
}

}

第二次使用 implode 在数组中添加逗号。

$code = implode(",", $code);

然后添加Mysql查询。

"SELECT * FROM grobar_IDs WHERE fiscode IN ('".$code."'");

如果有任何问题,请告诉我。

关于php - while 循环中的 mysql_fetch_assoc 问题 - foreach 循环中的查询格式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48089806/

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