发布' *'的结果PHP中的SQL

时间:2012-11-25 23:40:38

标签: php mysql

这是我的代码段:

    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中检查了查询,运行正常。我无法弄清楚为什么这不会发布。

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:2)

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

while ($row = $stmt->fetch_row()){

答案 2 :(得分: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();
    ?>