简体   繁体   English

PHP MySQL:查询疑难解答

[英]PHP MySQL: Troubleshoot query

I have what I thought was a straight-forward query, but I cant seem to get it to work. 我有一个直截了当的查询,但是我似乎无法使其正常工作。 All help appreciated! 所有帮助表示赞赏!

The Tables: 表格:

There are two tables involved: gallery_meta and prm_album . 涉及两个表: gallery_metaprm_album The gallery_meta tbl contains the data for images stored in the file structure. The gallery_meta tbl包含文件结构中存储的图像数据。 The prm_album tbl simply aligns album ID's with album regular-text Names. prm_album tbl只是将专辑ID与专辑常规文本名称对齐。 For both tbls, the field names used in the query are correct. 对于这两个tbl,查询中使用的字段名称都是正确的。

The Query: 查询:

In the code below, I first get an array of saved album names. 在下面的代码中,我首先获得一个保存的专辑名称数组。 I then check the file structure to see which directories actually contain images. 然后,我检查文件结构以查看哪些目录实际包含图像。 At this point I want to query the db again for a thumbnail for each of the albums that return as positives. 在这一点上,我想再次查询数据库以获取每个返回正片专辑的缩略图。 It is here that the the process fails. 在这里,该过程失败。

All I get is a whitescreen, no error reporting whatsoever. 我得到的只是一个白屏,没有任何错误报告。 The array $albums is being filled successfully, and the $id variable is being passed to the $q query. 数组$albums已成功填充,并且$id变量被传递给$q查询。 $pattern is simply the file path to the parent directory where the album directories are stored. $pattern只是存储相册目录的父目录的文件路径。

            //Get album names from db
            $albums = array(); 
            $q="SELECT ID, Name FROM prm_album WHERE 1";
            $sql=mysql_query($q) or die(mysql_error());
            while($r=mysql_fetch_array($sql)) {
                $album = $r['Name'];
                $album_dir = $pattern.$album.'/';

                //check which albums actually contain images (dividing to account for thumbs, med, lrg.)
                $filecount = count(glob($album_dir.'*'))/3;

                //if images found, put album in array
                if($filecount >= 1) {
                    $albums[ $r['ID'] ] = $filecount;
                }
            }

            foreach($albums as $id => $filecount) {     
                $q="SELECT m.AlbumID, m.FileName, m.FileExt,
                        m.LegacyName, m.IsDefault,
                        p.Name
                        FROM gallery_meta AS m
                            LEFT JOIN prm_album AS p
                                ON m.AlbumID = p.ID

                        WHERE m.AlbumID = $id
                            AND Public = 1
                            ORDER BY IsDefault DESC
                            LIMIT 0,1";

                $sql=mysql_query($q) or die(mysql_error());
                while($r=mysql_fetch_array($sql)) {
                    $album=$r['Name'].'/';
                    $legacyname=$r['LegacyName'];
                    $filename=$r['FileName'].'-sm.';
                    $ext=$r['FileExt'];

                    echo '<img src="'$pattern.$album.$filename.$ext.'" />'.$legacyname.'<br/>';
                }
            }

A couple of points ${id} can be written this way in a string and maybe needs \\"${id}\\" but more likely Put all of the$q string on one line in the PHP code or the parser interpreter could trip over the newline characters in it and certainly the sql will. 可以以这种方式在字符串中写入两点$ {id},并且可能需要\\“ $ {id} \\”,但更有可能将所有$ q字符串放在PHP代码中的一行上,否则解析器解释程序可能会出错在其中的换行符上,当然SQL也会。

$q="SELECT m.AlbumID, m.FileName, m.FileExt, m.LegacyName, m.IsDefault, p.Name FROM gallery_meta AS m LEFT JOIN prm_album AS p ON m.AlbumID = p.ID WHERE m.AlbumID = $id AND Public = 1 ORDER BY IsDefault DESC LIMIT 0,1";

You should be able to test that on a command line into a database first too(bit of wisdom). 您也应该也可以先在命令行上将其测试到数据库中(明智之举)。

Change 更改

echo '<img src="'$pattern.$album.$filename.$ext.'" />'.$legacyname.'<br/>';

to

echo '<img src="'.$pattern.$album.$filename.$ext.'" />'.$legacyname.'<br/>';

The first dot was missing. 第一个点不见了。 And always set these lines on top of your script =) 并始终将这些行设置在脚本的顶部=)

ini_set('display_errors', 1);
error_reporting(E_ALL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM