while循环,当数据库有值提交到post发布时

时间:2014-06-15 07:32:57

标签: php mysql database

所以我试图在数据库为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());

我尝试从这里开始进行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>"

}
?>

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

1 个答案:

答案 0 :(得分:0)

我建议您最好开始使用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 … ?>标记。