1

我有这些表:

报告:

id, ip_addr, upload_id

上传:

id, userID, name, location, category, private

基本上我正在显示所有的上传,我想加入报告表以确定当前用户的 ip_address 是否已经报告了上传(我会在查询后比较 ip_addr)。我遇到的问题是同一上传可以有多个报告,并且由不同的人(不一定是注册用户,这就是我使用 ip_addr 的原因)。那么我将如何设置这个 MySQL 查询以及如何在循环中执行以下操作..?

PHP:($things 将是查询结果)

foreach((array)$things as $files){
    if ($files['ip_addr'] == $user_ip_addr) {
         // display upload info and an already reported image
    } else {
         // display upload info with an unreported image
    }
}

到目前为止,我有这个,它显示了所有的上传

$query = 'SELECT * FROM upload WHERE private="0" ';
4

1 回答 1

0

只是有多个标识符。表结构将与此类似:

表“报告”

reported_by = user id 
reported_id = upload id
id = primary index

您的查询可能是这样的:

SELECT COUNT(*) as `rows` WHERE reported_id = ? AND reported_by = ?

瞧,

<?php
    if($rows['rows'] > 0) // they reported it
        echo 'Tattle tale.';
    else
        echo 'Snitches get stitches.';
?>

? 是未命名的参数(假设您使用的是 mysqli 或 pdo)

于 2012-12-25T03:06:21.170 回答