Php 连接两个表以匹配整数以获得用户名
我不知道如何连接两个表并从不同的数据库表中选择不同的行 我想添加我的Php 连接两个表以匹配整数以获得用户名,php,mysql,sql,join,Php,Mysql,Sql,Join,我不知道如何连接两个表并从不同的数据库表中选择不同的行 我想添加我的users表,以便将id与post\u创建者匹配,然后从users表中获取id和post\u创建者编号匹配的用户名 因此,如果我的users表的id为10,那么这就是Jim的用户名。如果post_creator的值是10,我希望Jim能被找到并得到回应 现在这正在扼杀代码,我不知道我做错了什么 while ($row = $stmt->fetch()) { //added in topic title va
users
表,以便将id
与post\u创建者
匹配,然后从users
表中获取id和post\u创建者编号匹配的用户名
因此,如果我的users表的id为10,那么这就是Jim的用户名。如果post_creator的值是10,我希望Jim能被找到并得到回应
现在这正在扼杀代码,我不知道我做错了什么
while ($row = $stmt->fetch()) {
//added in topic title variable
$topic_title;
// foreach($stmt as $row) {
//Prepared SELECT stmt to get forum posts
$stmt2 = $con->prepare("SELECT forum_posts.id, forum_posts.category_id, forum_posts.topic_id, forum_posts.post_creator, forum_posts.post_content, forum_posts.post_date users.id
FROM forum_posts LEFT JOIN users WHERE forum_posts.category_id=? AND forum_posts.topic_id=? AND forum_posts.post_creator=? AND users.id=?");
if($stmt2===false) {
die();
} else {
//var_dump($stmt2);
$stmt2->bind_param("ii", $cid, $tid);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
if (!$stmt2) {
throw new Exception($con->error);
}
}
$num_rows2 = $stmt2->num_rows;
if($num_rows2) {
$count2=0;
while($stmt2->fetch()) {
$count2++;
$post_id;
$post_category_id;
$post_topic_id;
$post_creator;
$post_content;
$post_date = fixDate($post_date);
//$post_date;
echo "<tr><td valign='top' style='border: 1px solid #000000;'>
<div style='min-height: 125px;'>".$topic_title. "Post ".$count2."<br />
by ".$post_creator." - " .$post_date. "<hr />" . $post_content ."</div></td>
<td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
<tr><td colspan='2'><hr /></td></tr>";
}
while($row=$stmt->fetch()){
//添加在主题标题变量中
$topic_title;
//foreach($stmt作为$row){
//准备选择stmt以获取论坛帖子
$stmt2=$con->prepare(“选择forum_posts.id、forum_posts.category_id、forum_posts.topic_id、forum_posts.post_创建者、forum_posts.post_内容、forum_posts.post_date users.id
从forum_posts左侧加入用户,其中forum_posts.category_id=?和forum_posts.topic_id=?和forum_posts.post_creator=?和users.id=?”;
如果($stmt2==false){
模具();
}否则{
//var_dump($stmt2);
$stmt2->bind_参数(“ii”、$cid、$tid);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($post_id、$post_category_id、$post_topic_id、$post_creator、$post_content、$post_date);
如果(!$stmt2){
抛出新异常($con->error);
}
}
$num_rows2=$stmt2->num_rows;
如果($num_rows2){
$count2=0;
而($stmt2->fetch()){
$count2++;
$post_id;
$post_category_id;
$post_topic_id;
$post_创建者;
$post_内容;
$post_date=固定日期($post_date);
//$post_日期;
回声“
“$topic_title.”发布“$count2.”
作者:“$post\u创建者。”-“$post\u日期。”
“$post\u内容。”
用户信息在这里!
;
}
您可以加入表格…例如(加入用户、类别和主题)
选择
forum_posts.id post_id,
论坛_posts.category _idpost _category _id,
forum_posts.topic_id post_topic_id,
论坛帖子。帖子创建者,
论坛帖子。帖子内容,
论坛发布。发布日期,
论坛\帖子\类别。类别\名称,
论坛\发布\主题。主题\名称,
users.id user\u id,
users.username用户\用户名
从…起
论坛帖子
左连接
使用者
ON users.id=forum\u posts.post\u创建者
右连接
论坛(post)类别
在forum\u post\u categories.id=forum\u posts.category\u id上
右连接
论坛帖子主题
ON forum\u post\u topics.id=论坛\u posts.topic\u id
哪里
论坛帖子。类别id=?和
论坛帖子。主题id=?和
论坛帖子。帖子创建者=?和
users.id=?
注意:这是未经测试的。您可以加入表…例如(加入用户、类别和主题)
select u.*,fp.* from users u inner join forum_posts fp u.id=fp.post_creator
where fp.category_id=? AND fp.topic_id=? AND fp.post_creator=? AND u.id=?
选择
forum_posts.id post_id,
论坛_posts.category _idpost _category _id,
forum_posts.topic_id post_topic_id,
论坛帖子。帖子创建者,
论坛帖子。帖子内容,
论坛发布。发布日期,
论坛\帖子\类别。类别\名称,
论坛\发布\主题。主题\名称,
users.id user\u id,
users.username用户\用户名
从…起
论坛帖子
左连接
使用者
ON users.id=forum\u posts.post\u创建者
右连接
论坛(post)类别
在forum\u post\u categories.id=forum\u posts.category\u id上
右连接
论坛帖子主题
ON forum\u post\u topics.id=论坛\u posts.topic\u id
哪里
论坛帖子。类别id=?和
论坛帖子。主题id=?和
论坛帖子。帖子创建者=?和
users.id=?
注意:这是未经测试的
select u.*,fp.* from users u inner join forum_posts fp u.id=fp.post_creator
where fp.category_id=? AND fp.topic_id=? AND fp.post_creator=? AND u.id=?
更新的查询:
select u.user_name,fp.post_creator from
users u inner join forum_posts fp u.id=fp.post_creator
选中这个更新的,它只会给你用户名和帖子创建者
更新的查询:
select u.user_name,fp.post_creator from
users u inner join forum_posts fp u.id=fp.post_creator
选中此更新的,它只会为您提供用户名和post\u创建者尝试删除此代码部分
if($stmt2===false) {
die();
} else { }
简单地做:
$stmt2 = $con->prepare("SELECT forum_posts.id, forum_posts.category_id, forum_posts.topic_id, forum_posts.post_creator, forum_posts.post_content, forum_posts.post_date users.id
FROM forum_posts LEFT JOIN users WHERE forum_posts.category_id=? AND forum_posts.topic_id=? AND forum_posts.post_creator=? AND users.id=?");
$stmt2->bind_param("ii", $cid, $tid);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
if (!$stmt2) {
throw new Exception($con->error);
}
请尝试删除此代码部分
if($stmt2===false) {
die();
} else { }
简单地做:
$stmt2 = $con->prepare("SELECT forum_posts.id, forum_posts.category_id, forum_posts.topic_id, forum_posts.post_creator, forum_posts.post_content, forum_posts.post_date users.id
FROM forum_posts LEFT JOIN users WHERE forum_posts.category_id=? AND forum_posts.topic_id=? AND forum_posts.post_creator=? AND users.id=?");
$stmt2->bind_param("ii", $cid, $tid);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
if (!$stmt2) {
throw new Exception($con->error);
}
我如何才能让它查看帖子创建者是否=users.id?我在这里发布的查询最初只是为了从forum_posts数据库中获取信息,所以最好执行一个操作查询,只查找我需要的信息,如forum_posts.post_creator和users.id?我不知道你的意思。这是将用户名加入到结果中设置-它们总是匹配的。你确实有一个where子句用于
users.id
…也许你可以删除它,看看我在说什么。你会得到这样的结果:13 | john|u doe | 123 | 12 | 45 | 13 |一些内容| 2015-07-24 | category name |主题名
如何让它看起来像是post creator users.id?问题我在这里发布的最初目的是从forum_posts数据库中获取信息,所以最好进行一次操作查询,只查找我需要的信息,如forum_posts.post_creator和users.id?我不知道你的意思。这是将用户名加入结果集-它们总是匹配的。你确实有一个where子句用于用户。id
…也许你可以删除它,看看我在说什么。你会得到这样的信息:13 | john|u doe | 123 | 12 | 45 | 13 |一些内容| 2015-07-24 |类别名称|主题名称
我如何让它看起来像帖子创建者=用户。id?我在这里发布的查询最初只是为了从论坛中获取信息ts数据库,所以最好做一个SEROPATE查询,只查找我需要的信息,如forum_posts.post_creator和users.iddo你只想匹配没有任何where条件的行吗?可能是的……但我仍然需要cid和tid的where语句,因为我在文件中的其他事情中使用了这些语句。这就是为什么我想uld是否可以进行单独的查询?我只想找到post_creator与users.id匹配的位置,然后确定用户