Php 更改脚本的输出
我的表名为pank 我知道如何连接到数据库,但下面的编程给我带来了问题 我的桌子是:Php 更改脚本的输出,php,mysql,Php,Mysql,我的表名为pank 我知道如何连接到数据库,但下面的编程给我带来了问题 我的桌子是: id| stream | title | cast | director 1 | stream1 | title of the movie1 | cast1 | director1 2 | stream1 | title of the movie2 | cast2 | director2 3 | stream2 | title of the movie3 | cast3 |
id| stream | title | cast | director
1 | stream1 | title of the movie1 | cast1 | director1
2 | stream1 | title of the movie2 | cast2 | director2
3 | stream2 | title of the movie3 | cast3 | director3
我的PHP脚本:
$query = "SELECT * FROM pank";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo "<h2>".'Stream : ', $row['stream'], "</h2>",
"<br />",
"<h3>", 'Title of the movie : ', $row['title'], "</h3>",
"<h3>", 'cast : ', $row['cast'], "</h3>",
"<h3>", 'director : ', $row['director'], "</h3>"
;
}
上述php的输出为:
stream1
title of the movie1
cast1
director2
stream1
title of the movie2
cast2
director2
stream2
title of the movie 3
cast3
director3
我只是不想再次将输出标记为stream1
echo "<h2>Stream : ".$row['stream']. "</h2>";
echo "<h3>".$row['title']."</h3>";
echo "<h3>".$row['cast']."</h3>";
echo "<h3>".$row['director']."</h3>";
期望的结果
$query = "SELECT * FROM pank";
$result = mysql_query($query);
$arrayKeys = array();
while ($row = mysql_fetch_array($result))
{
if(!array_key_exists($row['stream'],$arrayKeys)){
echo "<h2>".'Stream : ', $row['stream'], "</h2>";
}
$arrayKeys[$row['stream']] = true;
echo "<br />",
"<h3>", 'Title of the movie : ', $row['title'], "</h3>",
"<h3>", 'cast : ', $row['cast'], "</h3>",
"<h3>", 'director : ', $row['director'], "</h3>";
}
不。它只会返回正确答案。请检查您的数据库?。可能是记录有误,请注意不要使用mysql。您的代码没有问题,一定是您保存的数据。您的问题不清楚。首先,这是一个太本地化的问题,可能没有进一步的兴趣,它的当前形式在这里的网站上。第二,根据您的数据,输出与您所说的不一样。不重新发布问题的完全重复这使问题变得过于复杂,而这主要是表中数据的问题。你可以从他的表格示例中看到,他的数据会让你的if语句有点多余。再看看if在做什么,给它一个+1,因为它与第一条评论相反。但是,为了得到一个好的答案,它应该有一个解释。根据他的表,他没有重复的流名称,将输出与他的数据进行比较,这就是为什么它不是必需的。如果他这样做了,他可以在他的SQL中留下join并对它们进行分组,而不是使用PHP。@Dave:演示的输入和输出显示了其他情况,尽管您已经正确指出他在表引号中输入了错误。这不会改变任何事,您只需将输出移到一行,而不是3行。@SunilKumar:绝对是胡说八道。您没有更改脚本的语义,并且,很好。@SunilKumar:您不认为如果,是错误的,那么会有完全不同的结果吗?@SunilKumar:谢谢MaggiQall您明白我的意思了这是我作为一个output@pankaj您可以在我的查询中添加order by,它也同样有效。谢谢你的否决票。
echo "<h2>Stream : ".$row['stream']. "</h2>";
echo "<h3>".$row['title']."</h3>";
echo "<h3>".$row['cast']."</h3>";
echo "<h3>".$row['director']."</h3>";
$query = "SELECT * FROM pank";
$result = mysql_query($query);
$arrayKeys = array();
while ($row = mysql_fetch_array($result))
{
if(!array_key_exists($row['stream'],$arrayKeys)){
echo "<h2>".'Stream : ', $row['stream'], "</h2>";
}
$arrayKeys[$row['stream']] = true;
echo "<br />",
"<h3>", 'Title of the movie : ', $row['title'], "</h3>",
"<h3>", 'cast : ', $row['cast'], "</h3>",
"<h3>", 'director : ', $row['director'], "</h3>";
}