好的,这是MySQL表结构:
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(1) NOT NULL AUTO_INCREMENT,
`username` varchar(25) NOT NULL,
`email` varchar(35) NOT NULL,
`password` varchar(50) NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
CREATE TABLE IF NOT EXISTS `papers` (
`p_id` int(1) NOT NULL AUTO_INCREMENT,
`user_id` int(1) NOT NULL,
`p_name` varchar(70) NOT NULL,
`p_authors` varchar(70) NOT NULL,
`p_cauthor` varchar(30) NOT NULL,
`p_jname` varchar(50) NOT NULL,
`p_date` varchar(10) NOT NULL,
`p_url` varchar(50) NOT NULL,
UNIQUE KEY `p_url` (`p_url`),
KEY `Foreign_Key` (`p_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=62 ;
ALTER TABLE `papers`
ADD CONSTRAINT `papers_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`);
使用这个PHP / MySQL查询,它只返回最后插入的行,我想列出所有这些:
$result = mysql_query("SELECT u.username, p.p_url FROM users AS u INNER JOIN papers AS p ON p.user_id = u.user_id WHERE u.username='{$_SESSION['user']}'");
$output = mysql_fetch_assoc($result);
if(is_array($output)) { ...
print_r ($output);
print_r只报告一个数组元素,一个最后插入的数组元素,我需要全部列出它们。我的查询有问题,但我无法弄清楚是什么。谢谢。
答案 0 :(得分:1)
试试这个
$result = mysql_query("SELECT u.username, p.p_url FROM users AS u INNER JOIN papers AS p ON p.user_id = u.user_id WHERE u.username='{$_SESSION['user']}'");
while($output = mysql_fetch_assoc($result))
{
if(is_array($output))
print_r ($output);
}
请求您使用PDO或MYSLI来避免SQL注入。