简体   繁体   English

检查另一个表中是否存在外键值?

[英]Check if a foreign key value exists in another table?

I wanna check whether a foreign key value exists in another table.我想检查另一个表中是否存在外键值。 What I'm trying to do is: I have a table named Gallery and another one named Posts .我想做的是:我有一张名为Gallery的桌子和另一张名为Posts的桌子。 Posts is the parent table, so Gallery bears the post_id foreign key. Posts 是父表,所以 Gallery 带有 post_id 外键。 On the posts.php page, since not every post has a gallery, I wanted to show a msg for each row, as in "has a gallery" or "does not have a gallery".posts.php页面上,由于不是每个帖子都有画廊,我想为每一行显示一个消息,如“有画廊”或“没有画廊”。 For instance: post id 5 has a gallery and so does post number 10, but post id 1 doesn't have a gallery nor does post id 3. I'm thinking of a way like: Gallery table, if a gallery_id row has a 'null' value on column post_id, then echo "does not have a gallery." Else echo "has a gallery"例如:post id 5 有一个画廊,post number 10 也有,但是 post id 1 没有画廊,post id 3 也没有。我正在考虑这样的方式: Gallery table, if a gallery_id row has a 'null' value on column post_id, then echo "does not have a gallery." Else echo "has a gallery" if a gallery_id row has a 'null' value on column post_id, then echo "does not have a gallery." Else echo "has a gallery" , on posts.php . if a gallery_id row has a 'null' value on column post_id, then echo "does not have a gallery." Else echo "has a gallery" ,在posts.php上。 Is there a way of checking if a certain value exists in another table?有没有办法检查另一个表中是否存在某个值?

SQL code: SQL 代码:

CREATE TABLE `gallery` (
 `gallery_id` int(11) NOT NULL AUTO_INCREMENT,
 `picture` varchar(200) NOT NULL,
 `post_id` int(11) NOT NULL,
 `gallery_date` datetime NOT NULL,
 PRIMARY KEY (`gallery_id`),
 KEY `post_id` (`post_id`),
 CONSTRAINT `gallery_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=48 DEFAULT CHARSET=utf8mb4

I just tried我刚试过

<?php
    $checkGallery = new Read();      
    $checkGallery->performQuery('gallery', "WHERE post_id = :post_id", "id_post={$post_id}");      
    if($checkGallery->result()):        
      $affirmative = $checkGallery->result(); 
?>

and then接着

<td class="values">
  <?= (  $affirmative ? 'This post has a gallery!' : 'No gallery enrolled in this post' );?> 
</td> 

Got no satisfactory results.没有得到满意的结果。 It omitted certain rows instead of echoing "No gallery enrolled in this post"它省略了某些行,而不是回显“此帖子中没有注册画廊”

Boom.繁荣。 I got it.我知道了。

I just performed a query " SELECT * FROM gallery WHERE id_post =:id_post " (considering we are calling the bindValue method on posts.php )and id_post = $id;我刚刚执行了一个查询“ SELECT * FROM gallery WHERE id_post =:id_post ”(考虑到我们在 posts.php 上调用bindValue方法)和 id_post = $id; ( So, SELECT * FROM gallery WHERE id_post = 5 or... WHERE id = 3 and so on and the results are store in the $checkGallery array.) 所以,SELECT * FROM gallery WHERE id_post = 5 或... WHERE id = 3 等等,结果存储在 $checkGallery 数组中。)

<?php
  if($checkGallery):
?>
    <td><?= This post has a gallery! ?></td>
<?php else: ?>
    <td><?= No gallery attached to this post.?></td>
<?php endif;?>

And each row returns either " this post has a gallery " or " No gallery attached to this post ", all dynamically.并且每一行都动态返回“此帖子有一个画廊”或“此帖子没有附加画廊”。 Thanks谢谢

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

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