0

フォルダにいくつかの画像があります。写真の名前をデータベースのテーブルに保存しました。今度は、次のようにテーブルから選択して写真を表示したいと思います。

$a=$_GET['nume'];


$c= "select * from angajati where afisare_angajat='".$a."'" ;
    $b = mysql_query($c);

    while($d = mysql_fetch_array($b)){
        echo "<img src='/wp-content/themes/veles/images/angajati/'.$d['afisare_angajat'].'' />";
    }

しかし、これには問題があるようです: '.$d['afisare_angajat'].' ..写真の名前を入れると表示されますが、このままにしておくと何も表示されません..

4

3 に答える 3

0

次のようにする必要があります。

<?php
// I do not know what database system and API you are using
$pic_name = QueryPictureNameFromDB();

// Output html code to properly display the image
?>
<img src="<?php echo $pic_name; ?>" />

結果の出力 (html) は次のようになります。

<img src="myPic1234.png" />

PHP はあらゆる形式のテキストを出力する可能性があり、テキストはクライアントによって異なる方法で解釈される可能性があることを理解する必要があります。あなたの場合、phpスクリプトを使用してhtmlを作成しています。そのhtml「テキスト」は、クライアントが解釈して見るものです。したがって、php スクリプトでは、html を出力して、ユーザーに表示されるもの (この場合は画像) を定義する必要があります。

画像名を照会する方法も適切ではありません。クエリでは、画像の名前は既にわかっています。次のようなものが必要です。

SELECT * FROM table WHERE id=required_img_id
于 2012-09-25T08:38:52.813 に答える
0

scandirなどの関数を使用して 、フォルダーからファイル名を取得します。データベースで他のファイルタイプを探すのを避けるために、それらをフィルター処理することができます。次のようなもの。

/* open database connection */
$link = mysqli_connect("localhost", "my_user", "my_password", "images");

/* check connection */
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}

/* set the directory for your image files */
$dir    = '/images';
/* get a list of all files in the directory */
$filenames = scandir($dir);

/* go through all filenames in the list */
foreach ($filename in $filenames){
   get_image_name($filename)
};   

/* function that prints the associated data for the given filename */

function get_image_name($filename){
   /* construct query for filename */
   $sql = "SELECT * from table WHERE name_image_from_table = '".$filename."'";
   /* run the query */
   if ($result = mysqli_query($link,$sql)){
       /* if the query went OK check if there was one result */
       if (mysqli_num_rows($result) == 1){
           /* put the result in an associative array with the column names as keys */
           $row = mysqli_fetch_assoc($result)
           /* output the html for displaying the image and its name */
           echo "<img src='".$dir."/".$filename."'/>".$row['ImageNameField']."<br/>";
       } 
   }
}
于 2012-09-25T09:30:54.923 に答える
0

これを実現する方法の例を次に示します。データベースからすべての結果を取得し、それらを連想配列に変換してから、ループして名前を出力します。

<?php
    $getImagesSql = 'select * from table where name_image_from_table = name_image_from_folder';
    $getImagesQuery = mysql_query($getImagesSql);

    while($getImagesAssoc = mysql_fetch_assoc($getImagesQuery)){
        echo '<img src="/directory/goes/here/'.$getImagesAssoc['imageNameCol'].'" />';
    }
于 2012-09-25T08:44:39.600 に答える