[英]prepared statement in the loop
I have a problem that i cant figure out for too long.我有一个问题,我无法弄清楚太久。 Basically I have table with usernames (1 unique username in the table) with 1 respective image file.
基本上我有一个带有用户名的表(表中的1个唯一用户名)和1个相应的图像文件。 Some of users have image file and some dont.
有些用户有图像文件,有些没有。 usernames are stored in the array $Users[].
用户名存储在数组 $Users[] 中。 I am trying to fetch filenames of images for each user using:
我正在尝试使用以下方法获取每个用户的图像文件名:
$stmt = $db->prepare("SELECT file_name FROM images WHERE BINARY username=?");
for($temp1=0; $temp1<count($Users); $temp1++){
$stmt->bind_param("s", $Users[$temp1]);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($ImgFileName);
$stmt->fetch();
$imageURL[$temp1]=$ImgFileName;
}
however this has annoying behavior.然而,这有令人讨厌的行为。 Say as code loops through, if user User[0] has $ImgFileName related to it, but User[1], User[2] etc doesnt, then $ImgFileName is used of the last user with available image, instead of null.
假设代码循环通过,如果用户 User[0] 具有与其相关的 $ImgFileName,但 User[1]、User[2] 等没有,则 $ImgFileName 用于最后一个具有可用图像的用户,而不是 null。 This happens until some user in the loop again has image filename in the table.
这种情况会发生,直到循环中的某个用户再次在表中具有图像文件名。 So if i print $imageURL[] array after loops goes through, it looks smth like:
因此,如果我在循环通过后打印 $imageURL[] 数组,它看起来像:
$Users[]=[user1,user2,user3,user4,user5]
$imageURL[]=[img001.png,img001.png,img001.png,img231.png,img124.png,img124.png]
instead of代替
$Users[]=[user1,user2,user3,user4,user5]
$imageURL[]=[img001.png,,,img231.png,img124.png,]
anyone knows why?有谁知道为什么?
The reason why is because you never reset $ImgFileName
inside the loop.原因是您从未在循环内重置
$ImgFileName
。
Put $ImgFileName = null;
把
$ImgFileName = null;
inside the loop.循环内。
You can also unset the variable, which will result in the same outcome.您也可以取消设置变量,这将导致相同的结果。
for($temp1=0; $temp1<count($Users); $temp1++){
$stmt->bind_param("s", $Users[$temp1]);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($ImgFileName);
$stmt->fetch();
$imageURL[$temp1]=$ImgFileName;
unset($ImgFileName);
}
An alternative is to store the file name in an array with the name as the index (along with ensuring that the image name is blanked out each time)...另一种方法是将文件名存储在一个数组中,并将名称作为索引(同时确保图像名称每次都被空白)......
for($temp1=0; $temp1<count($Users); $temp1++){
$ImgFileName = '';
$userName = $Users[$temp1];
$stmt->bind_param("s", $userName);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($ImgFileName);
$stmt->fetch();
$imageURL[$userName] = $ImgFileName;
}
If you also combine this with using in
(from How do you use IN clauses with mysqli prepared statements ) you could just fetch a list of the known users and filenames in one execution...如果您还将此与 using
in
结合使用(来自How do you use IN clauses with mysqli Prepared statements )您可以在一次执行中获取已知用户和文件名的列表...
$stmt = $db->prepare("SELECT username, file_name
FROM images
WHERE username in...
just loop through the results and add them into an array.只需遍历结果并将它们添加到数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.