简体   繁体   English

使用PHP和mySQL仅打印查询的一个结果?

[英]Print only one result of a query using PHP and mySQL?

I am working with mySQL and PHP and I came across a query that is making think for the entire day, I'm not sure if my difficulties are with not knowing some advance command that might be requested for this, can you please help? 我正在使用mySQL和PHP,遇到了一个查询,整天都在思考,我不确定我的困难是否在于不知道可能需要为此提供一些高级命令,您能帮忙吗?

Here's the thing: I am working with an old database and I don't want to change the tables in it, so I got to work with two tables: 事情是这样的:我正在使用旧数据库,并且我不想更改其中的表,因此我必须使用两个表:

  • album: this table has only two fields, id_album and album_name; 专辑:此表只有两个字段,id_album和album_name;
  • photos: this table holds the photo's paths and it's linked to the album table by the field id_album; photos:此表保存照片的路径,并通过id_album字段链接到相册表;

I want to build a page that will list all the albums and each one will have a thumbnail, that I'm taking from the "photos" table, in that case, I need to list just one photo to each album, and I'm not sure how to write the code for that, this is what I got so far: 我想建立一个页面,列出所有相册,每个页面都有一张缩略图,该缩略图是我从“照片”表中获取的,在这种情况下,我只需要在每个相册中列出一张照片,我不确定该如何编写代码,这是我到目前为止得到的:

  <?php

  $comando = "SELECT * FROM album";

    $consulta = mysql_query($comando, $database)

        or die('Error:<br/>'.$comando);

    while($dados = mysql_fetch_array($consulta)) {


        $comando1 = "SELECT * FROM photos WHERE album = '".$dados['id_album']."'";
        $consulta1 = mysql_query($comando1, $database)
        or die('Error'.$comando1);

        while($dados1= mysql_fetch_array($consulta1)){          
          print '<li><a href="photos.php?Cod='.$dados['id_album'].'"><img src="'.$dados1['foto'].'"/></a><br/><a href="photos.php?Cod='.$dados['id_album'].'"> ' .$dados['album_name']. '</a></li>';

    }}

    ?>

It is working correctly but it is listing all photos in all albums. 它工作正常,但是列出了所有相册中的所有照片。 Is there a way that I can make it print just one photo for each album? 有什么办法可以让我为每张相册仅打印一张照片?

Thank you very very much in advance! 提前非常感谢您!

$comando1 = "SELECT * FROM photos WHERE album = '".$dados['id_album']."' LIMIT 1";

You should JOIN the queries. 您应该加入查询。 Use something like 使用类似

$comando = "
   SELECT album.id_album, photos.* FROM `album` 
   LEFT JOIN `photos` on photos.album = album.id_album
   GROUP BY album.id_album";
while($dados = mysql_fetch_array($consulta)) {

$comando1 = "SELECT * FROM photos WHERE album = '".$dados['id_album']."' LIMIT 1 order by rand()";
$consulta1 = mysql_query($comando1, $database) or die('Error'.$comando1);

$dados1= mysql_fetch_row($consulta1);        
print '<li><a href="photos.php?Cod='.$dados['id_album'].'"><img src="'.$dados1['foto'].'"/></a><br/><a href="photos.php?Cod='.$dados['id_album'].'"> ' .$dados['album_name']. '</a></li>';

}

But you should really use MySqli or something else .. 但是您应该真正使用MySqli或其他方式。

Yes you use a single query with GROUP_CONCAT to get photos of album and SUBSTRING_INDEX to pick one photo 是的,您可以对GROUP_CONCAT使用单个查询来获取相册的照片,并使用SUBSTRING_INDEX来选择一张照片

SELECT a.* ,SUBSTRING_INDEX(GROUP_CONCAT(p.foto),',',1) AS foto
FROM album a
LEFT JOIN photos p ON(a.id_album = p.id_album)
GROUP BY a.id_album

You can also specify ORDER BY in GROUP_CONCAT by your auto increment column in photos table to pick latest or first one as GROUP_CONCAT(p.foto ORDER BY p.id DESC) .Also as tadman mentioned in comments you are using depreciated mysql_* family functions better to start with PDO 您还可以通过在照片表中的自动增量列在GROUP_CONCAT指定ORDER BY ,以选择最新的或第一个作为GROUP_CONCAT(p.foto ORDER BY p.id DESC) 。此外,正如评论中提到的tadman,您正在使用已贬值的mysql_*系列函数最好从PDO开始

<?php

$dbh = new PDO('mysql:host=yourhost;dbname=dbname', 'user', 'pass');
$sth = $dbh->prepare("SELECT a.* ,SUBSTRING_INDEX(GROUP_CONCAT(p.foto),',',1) AS foto
FROM album a
LEFT JOIN photos p ON(a.id_album = p.id_album)
GROUP BY a.id_album
");
$sth->execute();
$result = $sth->fetchAll(); 

foreach($result as $res){

  echo '<li><a href="photos.php?Cod='.$res['id_album'].'"><img src="'.$res['foto'].'"/></a><br/><a href="photos.php?Cod='.$res['id_album'].'"> ' .$res['album_name']. '</a></li>';
}
?>

PDO::errorInfo

Also during development you should care for errors before execute 同样在开发过程中,您应该在执行之前注意错误

$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); 
if (!$sth) { 
    echo "\nPDO::errorInfo():\n"; 
    print_r($dbh->errorInfo()); 
} 

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

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