用户 -id(pk) -名称 -用户名 -telno
useradvert -id(指数) 等.........
它是1对多的关系。从表用户到表useradvert。表用户假设根据与表用户中的id匹配的特定id从表useradvert中提取许多行。但是,它只显示useradvert的第一行。无论您如何使用不同的用户名登录,您始终会看到useradvert的第一行,而不是应该显示的行。对于你的信息,我是一个新手。任何帮助表示赞赏。 TQS。以下是摘录;
$query = "SELECT * FROM users,useradvert WHERE users.id=useradvert.id";
$stmt = $conn->prepare($query);
$stmt->execute();
$res = $stmt->get_result();
$row2 = $res->fetch_array();
$_SESSION['name2'] = $row2['name2'];
$_SESSION['color2'] = $row2['color2'];
$_SESSION['hobby2'] = $row2['hobby2'];
$_SESSION['radiobtn'] = $row2['radiobtn'];
$_SESSION['kupon'] = $row2['kupon'];
$_SESSION['image'] = $row2['image'];
$_SESSION['image2'] = $row2['image2'];
下面的是在同一页上提取 - 继续
<?php
//display record from table- useradveret -(child table)
// while($row = $res->fetch_array()){
echo $_SESSION['name2']."<br/>";
echo $_SESSION['color2']."<br/>";
echo $_SESSION['hobby2']."<br/>";
echo $_SESSION['radiobtn']."<br/>";
echo $_SESSION['kupon']."<br/>";
echo $_SESSION['image']."<br/>";
echo $_SESSION['image2']."<br/>";
// }
&GT;
useradvert的表查询
useradvert
CREATE TABLE IF NOT EXISTS `useradvert` (
`id` int(10) unsigned NOT NULL,
`name2` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`color2` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`hobby2` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`radiobtn` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`kupon` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`image` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`image2` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `useradvert`
--
INSERT INTO `useradvert` (`id`, `name2`, `color2`, `hobby2`, `radiobtn`, `kupon`, `image`, `image2`) VALUES
(97, 'testthree nickname', 'colorthree', 'hobbythree', 'female', '', 'uploads/testpic1.jpg', 'uploads/testpic2.jpg'),
(99, 'testfivenamecick', 'testfivehcolor', 'testfivecolor', 'female', '', 'uploads/testpic3.jpg', 'uploads/testpic4.jpg'),
(97, 'lagitestthree', 'trheecolor', 'threehobby', 'female', '', 'uploads/testpic5.jpg', 'uploads/testpic6.jpg');
--
-- Constraints for dumped tables
--
--
-- Constraints for table `useradvert`
--
ALTER TABLE `useradvert`
ADD CONSTRAINT `useradvert_ibfk_1` FOREIGN KEY (`id`) REFERENCES `users` (`id`);
答案 0 :(得分:0)
我猜您的SQL查询不正确。使用useradvert表的id字段,但这是该表的索引字段。那看起来不对。您应该在useradvert表中有一个外键userId用于连接。然后你查询应该看起来像这样:
SELECT * FROM users u
INNER JOIN useradvert ua ON u.id = ua.userId
然后你应该为用户获取所有useradvert字段。
这两个表应该是这样的:
表用户:
id | name | ...
1 | Ben | ...
2 | Markus | ...
表useradvert:
id | userid | ...
1 | 1 | ...
2 | 1 | ...
useradvert中的