加入3个表php + mysql,显示图像

时间:2011-03-05 17:33:15

标签: php mysql

好的,慢慢地学习php和mysql并且每一步都陷入困境。我希望你能提供帮助很简单!

我有3个表:主题(id,名称),专辑(id,title,theme_id)和图像(id,name,album_id,image_url)。

我有:

function find_themes()
{
  db_connect();

  $query = sprintf("SELECT * from themes order by id DESC",
                         mysql_real_escape_string($theme_id));

$result = mysql_query($query);
if(!$result)
  {
    return false;
  }

  $result = db_result_to_array($result);

  return $result;

}

function find_images_by_album($album_id)
{
  db_connect();

  $query = sprintf("SELECT images.id,
                           images.name,
                           images.url,
                           images.album_id,
                           albums.id,
                           themes.name as theme
                  FROM
                         images, themes, albums
                  WHERE
                         albums.theme_id = themes.id and images.album_id = albums.id
                         ",
                         mysql_real_escape_string($album_id));

$result = mysql_query($query);
if(!$result)
  {
    return false;
  }

  $result = db_result_to_array($result);

  return $result;

}

function find_albums_by_theme($theme_id)
{
  db_connect();

  $query = sprintf("SELECT albums.id,
                         albums.title,
                         albums.theme_id,
                         themes.name as theme
                  FROM
                         albums, themes
                  WHERE
                         theme_id = '%s' and albums.theme_id = themes.id
                         ",
                         mysql_real_escape_string($theme_id));

$result = mysql_query($query);
if(!$result)
  {
    return false;
  }

  $result = db_result_to_array($result);

  return $result;

}

我正在努力在主题和专辑中显示正确的图像。然而,信息有效。

    $theme = find_theme($_GET['theme_id']);
    $albums = find_albums_by_theme($_GET['theme_id']);
    $images = find_images_by_album($image['album_id']);
    $themes = find_themes();

    <?php foreach($albums as $album):?>
    <a href="index.php?view=show&id=<?php echo $album['id']; ?>" class="medium-pic"><img src="photos/<?php echo $image['url']; ?>/medium/<?php echo $image['name']; ?>.jpg" alt="<?php echo safe_output($album['title']); ?>" /></a>
</div>
<div class="medium-photo-info">              
<span class="title"><a href="index.php?view=show&id=<?php echo $album['id']; ?>"><?php echo safe_output($album['title']); ?></a> </span> |
</div>
    <?php endforeach; ?>

    <?php foreach($images as $image): ?>
            <li><img src="photos/<?php echo $image['url']; ?>/large/<?php echo $image['name']; ?>.jpg" alt="<?php echo safe_output($album['title']); ?>" /></li>
   <?php endforeach; ?>
            </ul>

进度说明:谢谢大家,所以我定义了

function find_images_by_album($album_id)
{
  db_connect();

  $query = sprintf("SELECT images.id,
                           images.name,
                           images.url,
                           images.album_id,
                           albums.id,
                           albums.theme_id,
                           themes.name as theme,
                           themes.id
                  FROM
                         images, themes, albums
                  WHERE
                         images.album_id = albums.id and albums.theme_id = themes.id and
                         albums.id = '%s' 
                         ",
                         mysql_real_escape_string($album_id));

$result = mysql_query($query);
if(!$result)
  {
    return false;
  }

  $result = db_result_to_array($result);

  return $result;

}

function find_image($id)
{
  db_connect();

  $query = sprintf("SELECT 
                         images.id,
                         images.album_id,
                         images.name,
                         images.url,
                         albums.id
                             FROM 
                               images, albums
                             WHERE
                               images.album_id = albums.id and
                               images.id = '%s'
                           ",
                         mysql_real_escape_string($id));

$result = mysql_query($query);
if(!$result)
  {
    return false;
  }

  $row = mysql_fetch_array($result);

  return $row;

}

    $image = find_image($_GET['id']);
$images = find_images_by_album($image['album_id']);
    $album = find_album($_GET['id']);
$albums = find_albums_by_theme($album['theme_id']);
    $theme = find_theme($_GET['theme_id']);
$themes = find_themes();

现在,如果我跑

 <?php foreach($images as $image): ?>
            <li><img src="photos/<?php echo $image['url']; ?>/large/<?php echo $image['name']; ?>.jpg" alt="<?php echo safe_output($album['title']); ?>" /></li>
   <?php endforeach; ?>

它只返回第一张专辑中的图像?

4 个答案:

答案 0 :(得分:0)

find_images_by_album()获取id参数,但从不使用它。否则查询似乎没问题(尽管您应该找到比手动查询参数转义更安全的解决方案,最好是参数化查询)。

答案 1 :(得分:0)

这看起来有点奇怪:

$ query = sprintf(“SELECT * from subject order by id DESC”,                          mysql_real_escape_string($ theme_id));

首先,$ theme_id的值来自哪里?你想在这做什么? (s)printf用于向格式字符串提供参数。这里有一个直字符串(没有'%'变量)。

命名有点奇怪 - 是$ theme_id一个id号码?如果是这样,你不需要逃避它 - 这是一个字符串。

答案 2 :(得分:0)

$theme = find_theme($_GET['theme_id']);
$albums = find_albums_by_theme($_GET['theme_id']);
$images = find_images_by_album($image['album_id']);
$themes = find_themes();

首先,$image在这里未定义。如果您在PHP中关闭了通知,则不会出现错误,但会调用find_images_by_album并将空字符串作为参数,并且肯定不会返回您想要的内容。

另一方面,即使$image['album_id']定义了WAS,如dopey指出的那样,您也不会在某些sprintf格式字符串中包含任何类型说明符。

$query = sprintf("SELECT images.id,
                           images.name,
                           images.url,
                           images.album_id,
                           albums.id,
                           themes.name as theme
                  FROM
                         images, themes, albums
                  WHERE
                         albums.theme_id = themes.id and images.album_id = albums.id
                         ",
                         mysql_real_escape_string($album_id));

这将始终返回相同的$查询。你可能想要这样的东西:

$query = sprintf("SELECT images.id,
                           images.name,
                           images.url,
                           images.album_id,
                           albums.id,
                           themes.name as theme
                  FROM
                         images, themes, albums
                  WHERE
                         albums.theme_id = themes.id and images.album_id = albums.id
                         and albums.id = %d
                         ",
                         mysql_real_escape_string($album_id));

答案 3 :(得分:0)

我的两分钱......除了Ryan Mitchell所写的内容:

1)将db_connect()调用移出函数并将其放在函数调用之前。每次要执行查询时都不需要调用MySQL connect:

db_connect();
$theme = find_theme($_GET['theme_id']);
$albums = find_albums_by_theme($_GET['theme_id']);

2)对于查询,我的偏好是使用以下技术而不是sprintf。我发现它更具可读性,如果你在执行查询之前需要做更多的逻辑,你会发现它总是在函数的同一个地方。 (主观方式)

function find_theme($id)
{
    $id = (int) $id;
    $query =
        "SELECT
            *
        FROM
            `themes`
        WHERE
            `id` = $id
        ORDER BY
            `id` DESC";
    $result = mysql_query($query);
    // ...
}