[英]I don't get all the data from mysql - PHP
我已经搜索了一段时间,但没有找到任何相关内容。 所以我的问题是我确实使用while()从mysql获取了所有数据。 但是,即使内容不同,我尝试获取的所有文章也只能显示为1条。 抱歉,要解释这一点并不容易,但请参见下面的图片:
我的数据库:
显示方式:
我的articleFunction.php代码:
// check if user is logged in to view the content:
if(!isset($_SESSION['loggedin']) && !isset($_SESSION['loggedinAdmin'])){
header("Location: login.php");
}else{
}
//
$sql = "SELECT * FROM `articles` ORDER BY id DESC";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
$displayUsername = $row['username'];
$displayArticleName = $row['name'];
$displayArticleDescription = $row['description'];
$fullArticle = 'Article name: '.$displayArticleName.'<br/> This article was posted by: '.$displayUsername.'<br/>'.$displayArticleDescription.'<hr/>';
}?>
//
我的articles.php:
<?php
///////////////////////////////////////////////////////////////////////////////////////
// UNFINISHED //
///////////////////////////////////////////////////////////////////////////////////////
session_start();
require_once 'connect.php';
include 'articlesFunction.php';
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Blog posts</title>
<link rel="stylesheet" href="css/articles.css">
</head>
<body>
<div id="bannerDiv" style="background-image: url(images/banner.jpg); background-size: 100% 100%; height:150px;">
<h2 id="bannerTitle"><u><i>Articles about travel that everyone loves...</i></u></h2>
<p><a href="index.php">Homepage</a></p>
<span id="BannerMenu"><?php echo 'logged in as: '.$_SESSION['username'].' ';?></span><a href="logout.php"><button>Logout</button></a>
</div>
<div id="container">
<div id="articles">
<div id="Display Articles">
<h1><u>Our set of articles:</u>
</h1>
<div id="display">
<?php
echo $fullArticle;
?>
</div>
</div>
</div>
</div>
</body>
</html>
因此,希望您现在了解我的问题。 首先,在这个示例中,当我将它单独放在div中时,我只会得到最旧的文章,而我只会得到1。如果在div中添加一会儿,它将得到上图中的结果:
因此,如何显示文章(所有文章),以及如何使每个文章与数据库中的文章不同。
您将在while
循环的每次迭代中覆盖变量。
while($row = mysql_fetch_assoc($result)){
$displayUsername = $row['username'];
$displayArticleName = $row['name'];
$displayArticleDescription = $row['description'];
$fullArticle = 'Article name: '.$displayArticleName.'<br/> This article was posted by: '.$displayUsername.'<br/>'.$displayArticleDescription.'<hr/>';
}
作为一个简单的例子
$a = 0;
$b = 3;
while($a < $b){
$output = $a;
$a++;
}
echo $output;
由于$output
每次都被覆盖,因此返回2
。 保留所有值有两种方法。
选项一,连接变量
$a = 0;
$b = 3;
$output = '';
while($a < $b){
$output .= $a;
$a++;
}
echo $output;
将输出012
。 在与.=
使用之前,我们必须定义变量。 使用.=
它试图首先连接该值,因此它必须已经存在。
选项二,将值存储在数组中
$a = 0;
$b = 3;
while($a < $b){
$output[] = $a;
$a++;
}
print_r($output);
这将输出:
Array
(
[0] => 0
[1] => 1
[2] => 2
)
这种方法需要更多的工作,因为当您以后要访问它时,必须重新进行遍历。 但是,如果您希望能够分别访问每个数据点,则可能会更好。
foreach($output as $value) {
echo $value;
}
还要注意,如果用户提供其用户名,文章名称或描述,并且您没有进行过滤,这将使您可以进行XSS注入。
https://zh.wikipedia.org/wiki/跨站点脚本
https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#A_Positive_XSS_Prevention_Model
您实际代码中的用法是:
$sql = "SELECT * FROM `articles` ORDER BY id DESC";
$result = mysql_query($sql);
$fullArticle = '';
while($row = mysql_fetch_assoc($result)){
$displayUsername = $row['username'];
$displayArticleName = $row['name'];
$displayArticleDescription = $row['description'];
$fullArticle .= 'Article name: '.$displayArticleName.'<br/> This article was posted by: '.$displayUsername.'<br/>'.$displayArticleDescription.'<hr/>';
}?>
这应该是您的代码,以避免出现注释中提到的错误;
//init fullname variable
$fullArticle = '';
while($row = mysql_fetch_assoc($result)){
$displayUsername = $row['username'];
$displayArticleName = $row['name'];
$displayArticleDescription = $row['description'];
$fullArticle .= 'Article name: '.$displayArticleName.'<br/> This article was posted by: '.$displayUsername.'<br/>'.$displayArticleDescription.'<hr/>';
}?>
//
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.