PHP MySQL查询与scandir列出两者

时间:2017-06-06 14:18:23

标签: php html directory

我有一个图像目录和一个数据库,具有相同的图像文件名。 我试图创建一个表格,显示第一列中的图像列表和文件名,在数据库中列出,等于下一列中的图像名称。 任何未列在数据库中的图像都将列在表格的结尾处或开头,显示网站未使用的图像。

//select the data from the database ready for calling
$sql = "SELECT * from userImages";
$result = mysql_query($sql);
if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;}
if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;}
//Table index row
echo "<table><tr><td>File</td><td>DbListing</td></tr>";
//collects the filenames from the directory
define('IMAGEPATH', 'preload-images-thumbs/');
if (is_dir(IMAGEPATH)){
    $handle = opendir(IMAGEPATH);}
else{echo 'No image directory';}
$directoryfiles = array();
while (($file = readdir($handle)) !== false) {
    $newfile = str_replace(' ', '_', $file);
    rename(IMAGEPATH . $file, IMAGEPATH . $newfile);
    $directoryfiles[] = $newfile;}
foreach(glob(IMAGEPATH.'*') as $filename){
$dir_image=basename($filename);
//lists the filenames from the directory in the first column
echo "<tr><td>".basename($filename)."</td><td>";
//lists the rows of image names from the database
while ($row = mysql_fetch_assoc($result)) {
  //this just dumps the whole list on the first row of the next column in the table
echo $row["Image_dir"];}
echo "</td></tr>";
    }

上面的代码段中发生的事情只是从第一行的数据库中转储列表。我是否需要爆炸这个?如何将每一行与存在的文件名匹配并在最后运行多余的图像?

所以在经过一些补充并稍微翻转代码之后,我想出了这个:

//select the data from the database ready for calling
$sql = "SELECT * from userImages";
$result = mysql_query($sql);
if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;}
if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;}
//Table index row
echo "<table><tr><td>File</td><td>DbListing</td></tr>";
//collects the filenames from the directory
define('IMAGEPATH', 'preload-images-thumbs/');
if (is_dir(IMAGEPATH)){
    $handle = opendir(IMAGEPATH);}
else{echo 'No image directory';}
$directoryfiles = array();
while (($file = readdir($handle)) !== false) {
    $newfile = str_replace(' ', '_', $file);
    rename(IMAGEPATH . $file, IMAGEPATH . $newfile);
    $directoryfiles[] = $newfile;}
foreach(glob(IMAGEPATH.'*') as $filename){
$dir_image=basename($filename);
//lists the filenames from the directory in the first column
echo "<tr><td>".basename($filename)."</td><td>";
//lists the rows of image names from the database
$sql = "SELECT * from userImages WHERE Image_dir='$filename'";
$result = mysql_query($sql);
if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;}
if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so exiting";
    exit;}
while ($row = mysqli_fetch_assoc($result or die(mysql_error()))); {
  //this just dumps the whole list on the first row of the next column in the table
 (explode(" ",$row["Image_dir"]));}
echo "</td></tr>";
    }

现在返回第一列目录中的第一个图像,但告诉我:

&#34;没有找到任何行,没有任何要打印的内容如此退出&#34;

......当它清楚地列在数据库中时......

| 图像目录 | 数据库列表 |

| Apples.jpg |找不到行,没有要打印的内容如此退出|

所以,看起来它已经到了某个地方,但还没到达那里: - /

0 个答案:

没有答案