-3

这是我的代码片段:

    if (!($stmt = $mysqli->prepare("SELECT * FROM CUSTOMER"))) {
            echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }
    if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    $stmt->store_result();

        echo "<table border=2 cellpadding=4>
        <tr bgcolor=white>
        <th>Name</th>
        <th>Lname</th>
        <th>Phone</th>
        <th>Address</th>
        <th>Discount</th>
        <th>email</th>
        </tr>";
        while ($row = mysql_fetch_array($stmt)){
            echo "<tr>
            <td>".$row['Name']."</td>
            <td>".$row['Lname']."</td>
            <td>".$row['Phone']."</td>
            <td>".$row['Address']."</td>
            <td>".$row['Discount']."</td>
            <td>".$row['email']."</td>
            </tr>";
        }
        echo "</table>";

    $stmt->free_result();

$mysqli->close();
?>

意思是 mysql_fetch_array() 期望参数 1 是资源。我检查了 myPHPadmin 中的查询,它运行良好。我不明白为什么这不会发布。

4

3 回答 3

4

mysql_fetch_array来自“mysql”扩展,它不同于其余代码使用的“mysqli”扩展。有关 mysqli 中准备好的语句的完整示例,请参见http://php.net/manual/en/mysqli.prepare.php

我想你想要这样的东西(用 标记的更改***):

if (!($stmt = $mysqli->prepare("SELECT * FROM CUSTOMER"))) {
        echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}

$result = $stmt->get_result(); //***

    echo "<table border=2 cellpadding=4>
    <tr bgcolor=white>
    <th>Name</th>
    <th>Lname</th>
    <th>Phone</th>
    <th>Address</th>
    <th>Discount</th>
    <th>email</th>
    </tr>";

    while ($row = $result->fetch_assoc()){  //***
        echo "<tr>
        <td>".$row['Name']."</td>
        <td>".$row['Lname']."</td>
        <td>".$row['Phone']."</td>
        <td>".$row['Address']."</td>
        <td>".$row['Discount']."</td>
        <td>".$row['email']."</td>
        </tr>";
    }
    echo "</table>";

$stmt->close(); //***

$mysqli->close();
于 2012-11-25T23:49:01.880 回答
2

您在mysqli对象上使用 mysql_* 函数,您需要使用mysqli_result::fetch_row.

while ($row = $stmt->fetch_row()){
于 2012-11-25T23:48:37.183 回答
0

我能够基于此回答我自己的问题:

http://www.php.net/manual/en/mysqli-result.fetch-array.php

不确定这是否是最好的风格,但它确实有效。下面是完整的php

    <?php
    ini_set('display_errors', 'On');
    $mysqli = new mysqli("***", "***", "***", "***");
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }
        $stmt = "SELECT * FROM CUSTOMER";

        if (!$result = $mysqli->query($stmt)) {
            echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        }

            echo "<table border=2 cellpadding=4>
            <tr bgcolor=white>
            <th>Name</th>
            <th>Lname</th>
            <th>Phone</th>
            <th>Address</th>
            <th>Discount</th>
            <th>email</th>
            </tr>";
            while ($row = $result->fetch_array(MYSQLI_ASSOC)){
                echo "<tr>
                <td>".$row['Name']."</td>
                <td>".$row['Lname']."</td>
                <td>".$row['Phone']."</td>
                <td>".$row['Address']."</td>
                <td>".$row['Discount']."</td>
                <td>".$row['email']."</td>
                </tr>";
            }
            echo "</table>";

        $result->free();

    $mysqli->close();
    ?>
于 2012-11-26T00:18:54.723 回答