0

ファイル名の一部をphpのmy sqlクエリのパラメータとして渡そうとしています。私のファイル名は db_feature_T1..iOXXdrXXcAMEra_092521.txt の形式で、そこから db_feature と .txt を除く部分、つまり T1..iOXXdrXXcAMEra_092521 が必要です。

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 をリソース、ブール値で指定された C:\wamp\www\FashionBonanza\Readfile.php の 34 行目に表示します

どんな助けでも

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

たった2つのアイデア:

$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 に答える