mysqli如何在PHP / Issue中显示查询结果并放置PHP代码

时间:2016-04-06 17:25:50

标签: php mysqli

<!DOCTYPE html>
<html>
<head>
    <title> Search </title>
</head>

    <body>
    <h1>Please enter in the desired search criteria</h1>
    <form>
        Customer's First name:
        <br>
        <input type="text" name="Fname">
        <br>
        Customer's Last name:
        <br>
        <input type="text" name="Lname">
        <br> <br>
        Order ID:
        <br>
        <input type="text" name="OrderID">
        <br> <br>
        Volunteer's First Name:
        <br>
        <input type="text" name="VolFName">
        <br>
        Volunteer's Last Name:
        <br>
        <input type="text" name="VolLName">
        <br><br>
        Item ID:
        <br>
        <input type="text" name="ItemName">
        <br><br>
        <input type="submit" name="submit_search" value="submit_search">
    </form>
    </body>
<?php
    $fName = $_POST['Fname'];
    $lName = $_POST['LName'];
    $orderID = $_POST['OrderID'];
    $VolFName = $_POST['VolFName'];
    $VolLName = $_POST['VolLName'];
    $itemName = $_POST['ItemName'];

    //Connect to server
    $conn = new mysqli('localhost', 'user', 'password', 'KittenMittens');
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    /*
    $sql = "SELECT * FROM Customer where ('$fName' = fName and '$lName' = lName)";
    $rs = $conn->query($sql);
            //echo $rs;
    echo "<p align='center'><font color='red'><u>Results: </u></font></p>";
    echo $rs;
    if ($rs -> num_rows > 0){
        while($row = $rs -> fetch_assoc($rs)){
            echo $rows;
        }
    }
    */
    $sql = "SELECT * FROM KittenMittens.Customer";
    $result = $conn->query($sql);
    var_dump($result);
    while($row = $result->fetch_assoc()){
        echo $row['itemname'];
    }
    $conn->close();
?>
</html>

我一直在寻找W3Schools的不同教程和其他有用的指南,介绍如何从数据库中返回查询结果。当我运行var_dump($result)时,我得到:object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(9) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) },但我无法在屏幕上回显任何内容。我已经验证了我正在访问的表中有数据,并且验证了连接。

任何有助于弄清楚为什么我无法获得任何数据回应的帮助将非常感激。谢谢!

1 个答案:

答案 0 :(得分:0)

您需要在<body></body>标记

中回显