试图获取PHP的非对象属性

时间:2015-09-29 03:18:18

标签: php sql user-input

我是PHP新手并继续获得“试图获取非对象属性”错误。我的目标是允许用户键入客户ID并返回该特定客户的信息。我的代码在下面,并且都在一个PHP文件中。该错误表明我在第34行(具有"while($row = mysqli_fetch_assoc($result)) {"的行)上收到错误。

<!doctype html>
<html lang="en-US">
    <Head>
    </Head>
<Body>
    <style> table, TH {width: 500px; height: 20px;} </style>
    <form action="php.php" method="post">
    Customer ID: <input type="text" name="customerid"><br>
    <input type="submit">
    </form>

    <?php
        if(isset($_POST['customerid'])) {
            $servername = "localhost";
            $username = "test";
            $password = "";
            $dbname = "test";
            $cust = mysql_real_escape_string($_POST['customerid']);

            // Create connection
            $conn = mysqli_connect($servername, $username, $password);

            // Check connection
                    if (!$conn) {
                    die("Connection failed: " . mysqli_connect_error());
                    }
                    echo "<Table>". "<TH>Customer Name</TH>"."<TH>barr</TH>"."<TH>Cusomter Id</TH></Table>";
                    $sql = "SELECT customer_name, barr, customer_id FROM churn.data WHERE customer_id =". $cust ."";
                    $result = mysqli_query($conn, $sql);

                    if (mysqli_num_rows($result) > 0) {
                        // output data of each row
                        while($row = mysqli_fetch_assoc($result)) {
                        echo "<Table><TH>". $row["customer_name"]. "</TH><TH>". $row["barr"]."</TH><TH>". $row["customer_id"]."</TH></Table>";
                        }
                    }
                    else {
                        echo "0 results";
                    }
            mysqli_close($conn);
        }   
    ?>  
</body>
</html>

我改变了一些信息,因为我担心隐私,所以希望这不会影响任何事情 任何帮助将不胜感激。
感谢

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码而不是while循环。

$row = mysqli_fetch_assoc($result)) 

echo "<Table><TH>". $row["customer_name"]. "</TH><TH>". $row["barr"]."</TH><TH>". $row["customer_id"]."</TH></Table>";