简体   繁体   English

查询无法通过php脚本工作

[英]Query doesn't work via php script

I have a simple php script which performs a simple SQL query twice. 我有一个简单的php脚本,它执行了两次简单的SQL查询。 Query works perfectly for the first but for the second time error appears; 查询第一次可以完美地工作,但是第二次出现错误;

Notice: Trying to get property of non-object in C:\\apache\\localhost\\www\\study.kg\\webstore\\web_store.php on line 48 注意:试图在第48行的C:\\ apache \\ localhost \\ www \\ study.kg \\ webstore \\ web_store.php中获取非对象的属性

Fatal error: Call to a member function free() on a non-object in C:\\apache\\localhost\\www\\study.kg\\webstore\\web_store.php on line 80 致命错误:在第80行的C:\\ apache \\ localhost \\ www \\ study.kg \\ webstore \\ web_store.php中的非对象上调用成员函数free()

First, we make a query to 'categories' table. 首先,我们查询“类别”表。 Everything is good. 万事皆安。 Second, we make the same query to 'shoes' table. 其次,我们对“鞋子”表进行相同的查询。 Everything is bad, because 一切都不好,因为

$query = "SELECT * FROM shoes";
$result = $db->query($query);

returns FALSE value. 返回FALSE值。

But why? 但为什么? The first it worked perfectly. 首先,它运行良好。 The tables are the same, if you mean encoding and etc. So, as you can guess, my first table displays itself perfectly, second table doesn't show any data at all. 这些表是相同的(如果您指的是编码等)。因此,您可以猜到,我的第一个表完美显示了自己,第二个表根本不显示任何数据。 Here is a full snippet. 这是完整的代码段。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Лабораторная: интернет-магазин обуви</title>
</head>
<body>
<?php
@$db = new mysqli('localhost', 'dzhakipov', 'asd12345', 'webshop');

if (mysqli_connect_errno()) {
    echo 'Error: could not connect to database. Please try again later.';
    exit;
}

$query = "SELECT * FROM categories";
$result = $db->query($query);

$num_results = $result->num_rows;

?>
<table>
    <tr>
        <td>id</td>
        <td>Название</td>
        <td>Описание</td>
        <td>Поставщик</td>
        <td>Продавец</td>
        <td>Адрес склада</td>
    </tr>
    <?php
    for ($i = 0; $i < $num_results; $i++) {
        $row = $result->fetch_assoc();
        echo "<tr>";
        echo "<td>" .stripslashes($row['id']). "</td>";
        echo "<td>" .htmlspecialchars(stripslashes($row['name'])). "</td>";
        echo "<td>" .stripslashes($row['description']). "</td>";
        echo "<td>" .stripslashes($row['supplier']). "</td>";
        echo "<td>" .stripslashes($row['salesman']). "</td>";
        echo "<td>" .stripslashes($row['storage_address']). "</td>";
        echo "</tr>";
    }
    echo "</table>";

    $query = "SELECT * FROM shoes";
    $result = $db->query($query);

    $num_results = $result->num_rows; // LINE 48 #############################
    ?>
    <table>
        <tr>
            <td>id</td>
            <td>categoryid</td>
            <td>Название</td>
            <td>Пол</td>
            <td>Размер</td>
            <td>Сезон</td>
            <td>Внешний материал</td>
            <td>Внутренний материал</td>
            <td>Цвет</td>
            <td>Описание</td>
        </tr>
        <?php
        for ($i = 0; $i < $num_results; $i++) {
            $row = $result->fetch_assoc();
            echo "<tr>";
            echo "<td>" .stripslashes($row['id']). "</td>";
            echo "<td>" .stripslashes($row['categoryid']). "</td>";
            echo "<td>" .htmlspecialchars(stripslashes($row['name'])). "</td>";
            echo "<td>" .stripslashes($row['sex']). "</td>";
            echo "<td>" .stripslashes($row['size']). "</td>";
            echo "<td>" .stripslashes($row['season']). "</td>";
            echo "<td>" .stripslashes($row['inner_material']). "</td>";
            echo "<td>" .stripslashes($row['outer_material']). "</td>";
            echo "<td>" .stripslashes($row['colour']). "</td>";
            echo "<td>" .stripslashes($row['price']). "</td>";
            echo "</tr>";
        }
        echo "</table>";
        $result->free(); // LINE 80 #############################
        $db->close();
        ?>

</body>
</html>

Thanks for attention. 感谢您的关注。

See this http://php.net/manual/en/function.mysql-free-result.php before using result for the second time. 在第二次使用结果之前,请参阅此http://php.net/manual/zh/function.mysql-free-result.php

You also can change the second $result into $result2 to let It work. 您还可以将第二个$ result更改为$ result2以使其工作。

I'd try freeing the first result before querying for the second 我会尝试在查询第二个结果之前释放第一个结果

$result->free();
$query = "SELECT * FROM shoes";
$result = $db->query($query);

Second, though I'm not familiar with mysqli (big PDO fan here), I believe false might be a possible result of a query, so you should wrap the result traversing in an if block. 其次,尽管我不熟悉mysqli(这里是PDO的大粉丝),但我相信false可能是查询的结果,因此您应将遍历结果包装在if块中。

if($result) {
    $num_results = $result->num_rows;
    ...
    $result->free();
}

现在,当您将MySQL查询作为字符串存储在$ Query变量中以使php执行此查询时,您需要使用mysqli_query()从数据库检索数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM