简体   繁体   English

在MySQL中联接多个表

[英]Joining multiple tables in mySQL

Okay, I'm not that good at mySQL. 好的,我不太擅长使用MySQL。 What I'm trying to do here is join 2 tables: 1. users 2. comments I'm trying to make a comment system where it should pull the username and profile picture from users table and the comments and date_posted from the comments table. 我在这里想要做的是联接2个表: 1. users 2. comments我正在尝试创建一个注释系统,在该系统中应从users表中提取用户名和个人资料图片,并从comments表中获取注释和date_posted。

Here is my query: 这是我的查询:

$mem_query = mysql_query("SELECT `comments`.`comment_id` AS `comments_id`, `users`.`user_id` AS `users_id`, `users`.`username`,`users`.`profile_pic`,`comments`.`txt_content`,`comments`.`date_posted` FROM `comments` INNER JOIN `users` ON `users`.`user_id` = `comments`.`user_id` WHERE `comments`.`post_id` = '$post_id'");

And I want to run the query using the while loop: 我想使用while循环运行查询:

while($run_mem = mysql_fetch_array($mem_query)){
    $comment_id = $run_mem['comments_id'];
    $txt_content = $run_mem['comments.txt_content'];
    $profile_pic = $run_mem['users.profile_pic'];

?>
    //Run all the comments depending upon the post_id.
<?php
        }
?>

As of now, it is giving me this error: - THIS IS NOT SHOWING AFTER my 2nd update. 截至目前,它给了我这个错误:-第二次更新后没有显示。

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\simpleblog\view.php on line 73

How do I fix it? 我如何解决它? Thanks. 谢谢。

PS: I know 'mysql_query' is being deprecated in PHP. PS:我知道'mysql_query'在PHP中已被弃用。 I'll change that later. 我稍后再更改。 PS 2: I fixed the query from table.column to table . PS 2:我修复了从table.columntable的查询。 column , however, its not showing any errors but its not pulling any information from the database. column ,然而,它没有显示任何错误,但它不拉从数据库中的任何信息。

Look at the ` symbols, they should look like: 看一下`符号,它们应该看起来像:

`table`.`column`

and not: 并不是:

`table.column`

there is a big syntax error in your query: 您的查询中有一个很大的语法错误:

SELECT `comments.comment_id` AS `comments_id`, `users.user_id` AS `users_id`, `users.username`,`users.profile_pic`,`comments.txt_content`,`comments.date_posted` FROM `comments` INNER JOIN `users` ON `users.user_id` = `comments.user_id` WHERE `comments.post_id` = '$post_id'

should be 应该

SELECT `comments`.`comment_id` AS `comments_id`, `users`.`user_id` AS `users_id`, `users`.`username`,`users`.`profile_pic`,`comments`.`txt_content`,`comments`.`date_posted` FROM `comments` INNER JOIN `users` ON `users`.`user_id` = `comments`.`user_id` WHERE `comments`.`post_id` = '$post_id'

you wrote this: `comments.user_id` but it has to be this: `comments`.`user_id` and that at almost every position where you did that wrong 您写了这个:`comments.user_id`,但必须是这样:`comments`.`user_id`,而且几乎在您犯错的每个位置

http://www.php.net/manual/en/function.mysql-query.php http://www.php.net/manual/zh/function.mysql-query.php

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error. 对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他返回结果集的语句,mysql_query()成功时返回资源,错误时返回FALSE。

Looks like the latter happened. 看起来后者发生了。 An error occurred and the mysql_query call returned false. 发生错误,并且mysql_query调用返回false。

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

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