gpt4 book ai didi

php - while 循环用于当数据库有值提交到 post 到 div 时

转载 作者:行者123 更新时间:2023-11-30 00:09:14 25 4
gpt4 key购买 nike

所以我试图在数据库有为actual_quote和poster提交的值时执行一个while循环,当它有这些值时,它会发布显示这些值的div,并且为每一行放置一个新的div那一排。我不知道该去哪里,有什么帮助吗?

 <div class="wrapper">
<?php $db_name = 'submissions';
$db_user = 'root';
$db_pass = '';
$db_host = 'localhost';
mysql_connect("localhost", $db_user, $db_pass) or die(mysql_error());
mysql_select_db("submissions") or die(mysql_error());

我尝试从这里开始 for while 循环

while($info = mysql_fetch_array($actual_quote, $poster))
{
echo"
<div class="submissions">
<div class="logo-logo"><h2>Questions.</h2>
<div class="checkboxes"><?= !empty($_GET['x']) ? $_GET['x'] : '' ?>
</div>

</div>
<?php
$actual_quote = mysql_query("SELECT actual_quote FROM data");
$info = mysql_fetch_array( $actual_quote );
?>
<div class="top-submit">
<?php echo '&#8220;' . $info['actual_quote'] . '&#8221;'; ?>
</div>
<?php
$poster = mysql_query("SELECT poster FROM data");
$info = mysql_fetch_array( $poster );
?>
<div class="poster"><?php echo '-' . $info['poster']; ?>
<div class = "like">
<a href = "javascript:countClicksLike();" class = "btn btn-large" style = "color:green;">Like</a>
<p id = "like" style = "color:green;">0</p>
</div>
<div class = "dislike">
<a href = "javascript:countClicks();" class = "btn btn-large" style = "float:right; color:red;">Dislike</a>
<p id = "dis" style = "color:red;">0</p>
</div>
</div>
<!-- use select to get the items to stay on the page-->

</div>
</div>"

}
?>

但我完全迷失了。有什么建议吗?

最佳答案

我建议你最好开始使用PHP的PDO而不是mysql扩展,它会为你省去很多麻烦,而且语法和错误处理也更加清晰:

示例:

$DBH = new PDO('mysql:host=http://yourhost.com;dbname=yourdbname;charset=utf8', 'username', 'password');
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$STH = $DBH->prepare('SELECT * FROM `yourtable` WHERE `yourcolumn` = ?');
$STH->bindParam(1, $yourparam);
$STH->execute();

$STH->setFetchMode(PDO::FETCH_OBJ);

$mydata = array();
while ($var = $STH->fetch()) {
/* Retrieve the information */
$mydata[] = $var;
}

$DBH = null;

您可以使用prepared statements它会自动转义所有无效或有问题的字符。您还可以制作一个准备好的语句并重复使用它来插入大量数据,同时优化整个过程:

$DBH = new PDO('mysql:host=http://yourhost.com;dbname=yourdbname;charset=utf8', 'username', 'password');
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$STH = $DBH->prepare('INSERT INTO `yourtable` (`column1`, `column2`) VALUES (?, ?)');

foreach($array as $value) {
$STH->bindParam(1, $value);
$STH->bindParam(2, $value);
$STH->execute();
}

$DBH = null;

您可以在 phpro.org's PDO tutorial pages 上找到更多信息。它们解释得很好。

关于您的问题(使用 PDO 的示例):

<?php
$DBH = new PDO('mysql:host=http://yourhost.com;dbname=yourdbname;charset=utf8', 'username', 'password');
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$STH = $DBH->prepare('SELECT * FROM `yourtable` WHERE `yourcolumn` = ?');
$STH->bindParam(1, $yourparam);
$STH->execute();

$STH->setFetchMode(PDO::FETCH_OBJ);

/* Store your data here */
$mydata = array();
while ($var = $STH->fetch()) {
$mydata[] = $var;
}

$DBH = null;
?>

<html>
<head>
<!-- TODO html header stuff -->
</head>
<body>
<?php
/* And use it here */
foreach($mydata as $data) {
echo
'<div>
<span>'.$data->column1.'</span>
<span>'.$data->column2.'</span>
<span>'.$data->column3.'</span>
</div>';
}
?>
</body>
</html>

我建议您将脚本收集数据的部分和显示数据的部分分开,即查询位于标题(甚至 html 元素)之前。然后就可以打开<?php … ?>每当您想使用以前检索到的数据时,就可以添加标签。

关于php - while 循环用于当数据库有值提交到 post 到 div 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24227496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com