php - 这个 Mysql select,php 代码有什么问题?

标签 php mysql sql

当我尝试运行此代码时,没有显示任何结果。请帮助我

...这里有一些代码...

$result = mysql_query("select  *  from dataform where  date between '" . $d1 . "' and '" . $d2 . "'");

if (!$result) {
    echo 'No result';
}
else{
    echo $d1;
    echo $d2;
    echo "<table class=\"hovertable\">";

    echo "
<tr onmouseover=\"this.style.backgroundColor='#ffff66';\" onmouseout=\"this.style.backgroundColor='#d4e3e5';\">";

    echo "<th>File No</th>";
    echo "<th>Manufactor</th>";
    echo "<th>Address</th>";
    echo "<th>Supplier</th>";
    echo "<th>Place Site</th>";
    echo "<th>Tender Ref</th>";
    echo "<th>Award No</th>";
    echo "</tr>";
    while ($row = mysql_fetch_array($result)) {
        echo "<tr onmouseover=\"this.style.backgroundColor='#ffff66';\" onmouseout=\"this.style.backgroundColor='#d4e3e5';\">";
        echo "<th>" . $row["fileno"] . "</th>";
        echo "<th>" . $row["manufacture"] . "</th>";
        echo "<th>" . $row["address"] . "</th>";
        echo "<th>" . $row["sup"] . "</th>";
        echo "<th>" . $row["placesite"] . "</th>";
        echo "<th>" . $row["tenderref"] . "</th>";
        echo "<th>" . $row["awardno"] . "</th>";
        echo "</tr>";
    }

最佳答案

尝试使用反引号 ` 转义您的列 DATE,因为它是 MySQL 数据类型。另一个建议是避免使用查询的字符串连接,因为它很容易被 mysql 注入(inject)。使用 PHP PDO MYSQLi .

使用 PDO 的例子:

<?php
$stmt = $dbh->prepare("SELECT * FROM dataform WHERE  `date` BETWEEN ? AND ?");

$stmt->bindParam(1, $d1);
$stmt->bindParam(2, $d2);

$stmt->execute();

$result = $stmt->fetchAll(PDO::FETCH_ASSOC); //Fetch all results in form of associative array.

?>

请记住始终清理您的输入。

更新 1

您没有在 WHILE 循环中检查条件。您现在的做法是分配一个错误的值。尝试使用 ==

代替

while ($row = mysql_fetch_array($result))
{

}

改成这样

while ($row == mysql_fetch_array($result))
{

}

关于php - 这个 Mysql select,php 代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11923236/

相关文章:

php - PDO 和 MySQLi 是否使用抽象层下的 MySQL 函数?

mysql - SQL 不同的分组查询

c# - Csharp MySQL如何将变量插入查询

php - php中的分页限制数据但不显示在下一页中

php - 具有多个标签的 SQL 请求文件

php - 如何知道电子邮件地址是否无效?

php - 依赖脚本需要 Composer 自动加载器的路径

php - 解析 TWIG 中的变量内容

php - 使用php上传图片到sql数据库时显示错误

sql - VSTS 构建不会拾取 dacpac 文件(云中的托管代理)