根据数据库中的名称从文件夹中获取图像(php)
我的文件夹里有一些图像。我已将图片的名称存储在数据库的一个表中。现在,我想通过从表中进行选择来显示图片,如下所示:根据数据库中的名称从文件夹中获取图像(php),php,html,Php,Html,我的文件夹里有一些图像。我已将图片的名称存储在数据库的一个表中。现在,我想通过从表中进行选择来显示图片,如下所示: $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/theme
$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'].'' />";
}
<?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; ?>" />
SELECT * FROM table WHERE id=required_img_id
但这似乎有一个问题:“.$d['afisare_angajat']”如果我把它显示的图片的名称放进去,但如果我像这样离开它,什么都不显示。你应该这样做:
$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'].'' />";
}
<?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; ?>" />
SELECT * FROM table WHERE id=required_img_id
下面是一个如何实现这一点的示例。 从数据库中获取所有结果,将它们转换为关联数组,然后循环并输出名称
<?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'].'" />';
}
使用如下函数
要从文件夹中取出文件名,您可能需要对其进行筛选,以避免在数据库中查找其他文件类型。
类似下面的内容
/* 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/>";
}
}
}
问题到底出在哪里?你似乎很清楚该怎么做。。。您在哪里遇到了问题?您必须获取_assoc_数组,这样您将得到按列名索引的结果$d,$d['column_name']应返回与该键相关联的值/indexit不会为我带来相关的图像…如果我将其替换为。$getImagesAssoc['imageNameCol']。然后它会显示图片的名称,但如果我把它放进去,它不会将imageNameCol更改为存储图像名称的列。imageNameCol就是一个例子,如果它可以是任何东西的话。前端会出现html吗?您是否尝试过检查元素以查看文件的名称?另外,您是否在phpmyadmin中运行查询以查看是否返回任何结果?