我试图将一部分文件名作为参数传递给我的php中的sql查询。我的文件名在表单中 db_feature_T1..iOXXdrXXcAMEra_092521.txt 从中我想要除db_feature和.txt之外的部分,即 T1..iOXXdrXXcAMEra_092521
我想要数据库中的img_id,其img_path = dressimages / T1..iOXXdrXXcAMEra_092521.jpg
我的代码如下。我能够分隔文件名,但我无法从数据库中获得任何结果。
<?php
include('db.php');
if ($handle = opendir('C:\Users\adithi.a\Desktop\db_features')) {
while (false !== ($entry = readdir($handle))) {
$entry=substr($entry,11,-4); //this seperates the file name db_feature_T1..iOXXdrXXcAMEra_092521.txt to T1..iOXXdrXXcAMEra_092521
$image_path='dressimages/'.$entry.'.jpg';//I want to pass the img_path as it is saved in the database in the form of dressimages/T1..iOXXdrXXcAMEra_092521.jpg
$result= mysql_query("select img_id from tbl_image where img_path='$image_path'") or die("Sorry");
echo $result;//I dont get anything as output.
}
}
closedir($handle);
?>
我在执行上面的代码时进入了无限循环,所以我尝试了:
$image_path='dressimages/'.$entry.'.jpg';//I want to pass the img_path as it is saved in the database in the form of dressimages/T1..iOXXdrXXcAMEra_092521.jpg
$sql = "select img_id from tbl_image where img_path=".$image_path;
echo $sql . '<br />';
$result=mysql_query("$sql");
}
while($data=mysql_fetch_assoc($result))
{
echo $data["img_id"];
}
现在我得到错误mysql_fetch_assoc()期望参数1是资源,布尔值在第34行的C:\ wamp \ www \ FashionBonanza \ Readfile.php中给出
任何帮助
答案 0 :(得分:4)
您必须先fetch
结果中的数据
$data=mysql_fetch_assoc($result); // this is missing
print_r($data); // or
echo $data["img_id"];
如果可以有多个结果,你可以循环
while($data=mysql_fetch_assoc($result))
{
print_r($data); // or
echo $data["img_id"];
}
答案 1 :(得分:1)
因为你正在处理这么多文件,我会尝试另一种方法。那就是将所有路径存储到一个数组中,进行单个查询并将id与路径相关联。我不确定这段代码是否有效(我还没有测试过),但我希望你能得到这样的代码:
<?php
include('db.php');
if ($handle = opendir('C:\Users\adithi.a\Desktop\db_features')) {
//Store all paths into $image_paths array
$image_paths = array();
while (false !== ($entry = readdir($handle))) {
$entry=substr($entry,11,-4); //this seperates the file name db_feature_T1..iOXXdrXXcAMEra_092521.txt to T1..iOXXdrXXcAMEra_092521
if (strlen($entry) !== 0) {
$image_paths[]='dressimages/'.$entry.'.jpg';//I want to pass the img_path as it is saved in the database in the form of dressimages/T1..iOXXdrXXcAMEra_092521}
}
}
closedir($handle);
//Implode paths
$pathQuotesArray = array_map('apply_quotes', $image_paths); //Looks like 'filename1', 'filename2' etc
$pathQuotes = implode(',', $pathQuotesArray);
//Do one query
$result= mysql_query("select img_id from tbl_image WHERE img_path IN ($pathQuotes)") or die(mysql_error());
//Associate id's with paths
while($data=mysql_fetch_assoc($result))
{
$key[$data["img_id"]] = $data["img_path"];
}
echo $key[5]; //If img_id is 5, then it would show correct path (hopefully :-))
function apply_quotes($item)
{
return "'" . mysql_real_escape_string($item) . "'";
}
?>
答案 2 :(得分:0)
试试这个
$result= mysql_query("select img_id from tbl_image where img_path="$image_path) or die("Sorry");
答案 3 :(得分:-3)
只有两个想法:
$result= mysql_query("select img_id from tbl_image where img_path='".$image_path."'") or die("Sorry");
也许以这种方式做得更好。否则为SQL查询设置一个额外的字符串变量,这样就可以跟踪已执行的确切查询。
$sQry = "select img_id from tbl_image where img_path='".$image_path."'";
var_dump($sQry);
$result= mysql_query($sQry) or die("Sorry");
因此,您可以检查变量是否包含正确的值。
下一点是“回声”。您无法回显结果集,使用mysql-fetch-array()或mysql_fetch_assoc() - http://php.net/manual/de/function.mysql-fetch-array.php等函数或其他函数。
while ($row = mysql_fetch_assoc($result)) {
echo $row["img_id "];
}