从 UNION sql 中回显?
<?php
$sql = mysql_query("
SELECT navn FROM member_film WHERE username = '$pusername'
UNION SELECT meetup FROM member_meetups WHERE byusername = '$pusername'
UNION SELECT title FROM member_tutorials WHERE username = '$pusername'");
$rowit = mysql_fetch_array($sql);
$number = mysql_num_rows($sql);
?>
<? echo $number; ?>
<? echo $rowit["navn"]; ?><br>
<? echo $rowit["meetup"]; ?><br>
所以我有这个。 现在,我有 3 列,其中用户名是数据库中的用户名。 member_film 中为 2,member_meetups 中为 1
echo $number,显示它正确,我有 3。
现在,当我尝试回显“navn”时,它仅回显 1,当有 2 列时? 当我尝试回显“meetup”时,它显示未定义的索引:meetup,就像没有什么可显示的一样。 但是有一点,mysql_num_rows创建了它,否则它会是2?
我想你们大多数人都误解了我的意思.. 我想显示用户($pusername)在member_film、member_meetups、member_tutorials 中发布的内容。然后,如果用户的用户名有两列,我想显示同一列中的“navn”。与member_tutorials 上的标题以及member_meetups 上的meetup 相同。
我希望它看起来像这样的一个例子:
你的帖子:(正如我想要的那样,检查三个表中“用户名”列中是否有任何包含用户用户名的列。)
我的新视频,太棒了(navn,来自member_film)
我与父母的无聊聚会(标题,来自member_meetup)
我很棒的视频!(navn,来自member_film)
<?php
$sql = mysql_query("
SELECT navn FROM member_film WHERE username = '$pusername'
UNION SELECT meetup FROM member_meetups WHERE byusername = '$pusername'
UNION SELECT title FROM member_tutorials WHERE username = '$pusername'");
$rowit = mysql_fetch_array($sql);
$number = mysql_num_rows($sql);
?>
<? echo $number; ?>
<? echo $rowit["navn"]; ?><br>
<? echo $rowit["meetup"]; ?><br>
So i have this.
Now, i have 3 columns where username is username in the database.
2 in member_film, 1 in member_meetups
The echo $number, shows it correct, that i have 3.
Now when i try to echo the "navn", it echo only 1, when there is 2 columns?
And when i try to echo "meetup" it says undefined index: meetup, like there's nothing to show..
but there is, mysql_num_rows founded it, else it would have been 2?
I think most of you misunderstood me..
I want to show what the user($pusername) have been posted, in member_film, member_meetups, member_tutorials.. And then if e.g the user have 2 columns with his username, i want to display the "navn" from the same column. Same with title on member_tutorials, and meetup on member_meetups.
An example on how i want it to look like:
Your posts: (which as i want, checks the three table if there's any columns with the user's username in "username" column.)
My new video, its so good (navn, which is from member_film)
My boring meetup with my parents (title, which is from member_meetup)
My great great video!(navn, which is from member_film)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UNION 选择,将选择语句连接到 1 个结果集中,因此具有相同的列名。
正确的 SQL:
并在 php: 中
查找哪些表:
UNION selects, are joining select statements into 1 result set, therefor have the same column names.
Correct SQL:
and in the php:
and to find out what table:
联合查询将多个查询的结果组合成一个结果集。为了使联合工作,所有子查询都必须为查询的每个位置或每个命名字段返回具有相同数据类型的字段。
因此,您的原始查询将产生一个结果集,其中包含一个名为“navn”的字段(取自联合的第一个子查询)。因此,为了使其更具可读性,您可以在每个子查询上使用
As myName
重命名该字段(sam munkes 的解决方案)。但是,从您访问查询的方式来看,我相信您想要一行包含所有 3 个值,那么您应该尝试使用它:
在注释后编辑:
现在应该生成一个具有名为“comment”的列的表并且是 varchar(100)。您现在可以通过评论列访问所有值。如果您需要知道信息是从哪个表中获取的,请使用 Sam's< /a> 回答。我插入了转换以确保所有条目具有相同的数据类型和长度,如果它们位于您的数据库中,您可以省略这部分并仅使用名称。如果需要,需要调整长度。
需要说明的是,现在只有一栏名称为“评论”。
A union query combines the results of several queries into one resultset. In order for the union to work all subqueries have to return the fields with the same data types for each position of the query or each named field.
So your original query will result in a result set with one field called "navn" (taken from the first sub query of the union). So to make it more readable you could either rename the field using
As myName
on each of the subqueries (sam munkes' solution).However, from the way you are accessing the query I believe you want to have one row with all 3 values on it then you should try using this instead:
Edit after the comments:
This should now yield a table having one column named 'comment' and being of varchar(100). You can now access all values via the comment column. If you need to know from which table the information is taken, use Sam's answer. I inserted the convert to ensure that all entries are of the same data type and length, if they are in you DB you can leave this portion out and just use the names. If needed the length needs to be adjusted.
To make it clear, there is only one column with the name "comment" now.