PHP,MySQL和关联数组问题......?

时间:2016-11-22 08:19:09

标签: php mysql ajax

我跟着来自w3schools的example一起从mysql数据库中提取一些行并以表格格式返回结果,但我只得到空行(尽管它们的数量正确,奇怪的是。)

以下是我在php脚本中的内容:

<!DOCTYPE html>
<html>
    <head>
        <style>
            table
            {
                width: 100%;
                border-collapse: collapse;
            }
            table, td, th
            {
                border: 1px solid black;
                padding: 5px;
            }
            th
            {
                text-align: left;
            }
        </style>
    </head>

<body>
    <?php
        $q = $_GET['q'];

        $con = mysqli_connect('localhost', 'root', '123', 'drugsatfda');
        if (!$con) {die('Could not connect: ' . mysqli_error($con));}

        mysqli_select_db($con, "drugsatfda");
        $sql="SELECT * FROM products WHERE DrugName LIKE '"."%".$q."%"."'";
        $result = mysqli_query($con,$sql);

    $count = 0;

    echo "<table>
    <tr>
    <th>Application Number</th>
    <th>Product Number</th>
    <th>Form</th>
    <th>Strength</th>
    <th>Reference Drug</th>
    <th>Drug Name</th>
    <th>Active Ingredient</th>
    </tr>";

    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) && $count < 10) {
        echo "<tr>";
        echo "<td>" . $row['ApplNo'] . "</td>";
        echo "<td>" . $row['ProductNo'] . "</td>";
        echo "<td>" . $row['Form'] . "</td>";
        echo "<td>" . $row['Strength'] . "</td>";
        echo "<td>" . $row['ReferenceDrug'] . "</td>";
        echo "<td>" . $row['DrugName'] . "</td>";
        echo "<td>" . $row['ActiveIngredient'] . "</td>";
        echo "</tr>";
        $count++;
    }

    echo "</table>";

    mysqli_close($con);
?>
</body>
</html>

有人可以帮我看看我哪里出错吗?

2 个答案:

答案 0 :(得分:1)

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) && $count < 10) {

执行此操作时,您会得到两个条件,因此$row = mysqli_fetch_array($result, MYSQLI_ASSOC)只会成为返回布尔值true的表达式,因此当$count小于10时,您会得到while (true && true)

当循环达到10时,解决方案是break;

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    if ($count >= 10) // If we reach 10 iterations, break the loop
        break;

    echo "<tr>";
    echo "<td>" . $row['ApplNo'] . "</td>";
    /* and so on */ 
    echo "</tr>";
    $count++;
}

您可以通过执行

来验证这一点
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) && $count < 10) {
    var_dump($row);
}

每次迭代会输出bool(true);,直到无法获取更多行(mysqli_fetch_array()返回null时),或$count为10或更大时 - 以先到者为准。

更简单的方法

另一种选择,只是获取10行。您可以在SQL中添加LIMIT子句,例如

$sql="SELECT * FROM products WHERE DrugName LIKE '%".$q."%' LIMIT 10";

这将只获取10行,这意味着您可以正常循环,而不必计算

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    echo "<tr>";
    echo "<td>" . $row['ApplNo'] . "</td>";
    /* and so on */ 
    echo "</tr>";
}

还应该注意,您的代码目前对SQL注入是开放的,您应该使用预准备语句来防止这种情况。

参考

答案 1 :(得分:1)

Please try again...    

<!DOCTYPE html>
    <html>
        <head>
            <style>
                table
                {
                    width: 100%;
                    border-collapse: collapse;
                }
                table, td, th
                {
                    border: 1px solid black;
                    padding: 5px;
                }
                th
                {
                    text-align: left;
                }
            </style>
        </head>

    <body>
        <?php
            $q = $_GET['q'];

            $con = mysqli_connect('localhost', 'root', '123', 'drugsatfda');
            if (!$con) {die('Could not connect: ' . mysqli_error($con));}

            mysqli_select_db($con, "drugsatfda");
            $sql="SELECT * FROM products WHERE DrugName LIKE '"."%".$q."%"."'";
            $result = mysqli_query($con,$sql);

        $count = 0;

        echo "<table>
        <tr>
        <th>Application Number</th>
        <th>Product Number</th>
        <th>Form</th>
        <th>Strength</th>
        <th>Reference Drug</th>
        <th>Drug Name</th>
        <th>Active Ingredient</th>
        </tr>";

        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
            if($count >=10)
                break;
            echo "<tr>";
            echo "<td>" . $row['ApplNo'] . "</td>";
            echo "<td>" . $row['ProductNo'] . "</td>";
            echo "<td>" . $row['Form'] . "</td>";
            echo "<td>" . $row['Strength'] . "</td>";
            echo "<td>" . $row['ReferenceDrug'] . "</td>";
            echo "<td>" . $row['DrugName'] . "</td>";
            echo "<td>" . $row['ActiveIngredient'] . "</td>";
            echo "</tr>";
            $count++;
        }

        echo "</table>";

        mysqli_close($con);
    ?>
    </body>
    </html>
相关问题