0

我正在尝试将文件名的一部分作为参数传递给 php.ini 中的我的 sql 查询中的参数。我的文件名格式为 db_feature_T1..iOXXdrXXcAMEra_092521.txt

我想要 img_path=dressimages/T1..iOXXdrXXcAMEra_092521.jpg 的数据库中的 img_id

我的代码如下。我能够分隔文件名,但我无法从数据库中获得任何结果。

            <?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 中给出的布尔值

任何帮助

4

4 回答 4

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"];
}
于 2013-04-11T05:06:05.977 回答
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) . "'";
}



?>
于 2013-04-11T05:24:16.490 回答
0

尝试这个

$result=  mysql_query("select img_id from tbl_image where img_path="$image_path) or die("Sorry");
于 2013-04-11T05:06:34.350 回答
-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 "];
}
于 2013-04-11T05:06:53.453 回答