0

我目前正在使用数据库等。在一页上我的代码看起来像这样。我知道如何做准备好的语句,但是(在我的脑海中)无法弄清楚如何更改此查询以及在 bind_param 和 bind_result 等中放入什么。

任何帮助,将不胜感激。这是我的代码:

$topDate = date('Y-m-d', strtotime('-1 week'));
$query = "SELECT *, DATEDIFF(ends, starts) as datedifference FROM news WHERE DATEDIFF(starts,'$topDate')>0 ORDER BY starts LIMIT 12;";
if ($result = mysqli_query($connection, $query)) {
    while ($row = mysqli_fetch_assoc($result)) {
        //What i do with my data
    }
}
4

1 回答 1

0

+1 用于使用准备好的语句。

这是您的代码作为准备好的语句的示例(请记住,我不知道您的表结构是什么样的):

$connection = new mysqli(HOST,USER,PASSWORD,DATABASE);
$stmt = $connection->prepare("SELECT *, DATEDIFF(ends, starts) as datedifference FROM news WHERE DATEDIFF(starts,?)>0 ORDER BY starts LIMIT 12;");
$stmt->bind_param('s', $topDate);
$stmt->execute();
$stmt->bind_result($col1, $col2, $col3, $col4) //...etc, the number of variables here must match the column count;
if($stmt->num_rows > 0)
{
    while($stmt->fetch())
    {
        print("col1 = " . $col1, "col2 = " . $col2,"col3 = " . $col3,"col4 = " . $col4);
        //will bind the rows results to the $col variables on every pass.
    }



}

$stmt->close();
于 2013-11-11T20:38:00.483 回答