2016-08-09 93 views
0

我想從MySQL獲取一些數據並使用PHP進行回顯。以下是我使用的代碼。請檢查代碼並告訴我它有什麼問題。如何使用PHP從MySQL獲取數據?用我的代碼獲取錯誤

<?php 

    // Get a connection for the database 
    require_once('mysqli_connect.php'); 

    // Create a query for the database 
    $query = "SELECT first_name, last_name, email, street, city, state, zip, phone, birth_date FROM testable"; 

    // Get a response from the database by sending the connection and the query 
    $response = @mysqli_query($dbc, $query); 

    // If the query executed properly proceed 
    if($response){ 

    echo '<table align="left" 
    cellspacing="5" cellpadding="8"> 
    <tr><td align="left"><b>First Name</b></td> 
    <td align="left"><b>Last Name</b></td> 
    <td align="left"><b>Email</b></td> 
    <td align="left"><b>Street</b></td> 
    <td align="left"><b>City</b></td> 
    <td align="left"><b>State</b></td> 
    <td align="left"><b>Zip</b></td> 
    <td align="left"><b>Phone</b></td> 
    <td align="left"><b>Birth Day</b></td></tr>'; 

    // mysqli_fetch_array will return a row of data from the query until no further data is available 
    while($row = mysqli_fetch_array($response)){ 

    echo '<tr><td align="left">' . 
    $row['first_name'] . '</td><td align="left">' . 
    $row['last_name'] . '</td><td align="left">' . 
    $row['email'] . '</td><td align="left">' . 
    $row['street'] . '</td><td align="left">' . 
    $row['city'] . '</td><td align="left">' . 
    $row['state'] . '</td><td align="left">' . 
    $row['zip'] . '</td><td align="left">' . 
    $row['phone'] . '</td><td align="left">' . 
    $row['birth_date'] . '</td><td align="left">'; 
     echo '</tr>'; 
    } 
    echo '</table>'; 
    } else { 
    echo "Couldn't issue database query<br />"; 
    echo mysqli_error($dbc);  
    } 

    // Close connection to the database 
    mysqli_close($dbc); 
?> 

我得到這個代碼的輸出是:

名字姓氏電郵街道城市州郵編電話誕生日「; // mysqli_fetch_array將從查詢返回一行數據//直到 沒有更多數據可用時($ row = mysqli_fetch_array($ response)){echo''。 $ row ['first_name']。 ''。 $ row ['last_name']。 ''。 $ row ['email']。 ''。 $ row ['street']。 ''。 $ row ['city']。 ''。 $ row ['state']。 ''。 $ row ['zip']。 ''。 $ row ['phone']。 ''。 $ row ['birth_date']。 '';回聲''; } echo''; } else {echo「無法發出數據庫查詢」; echo mysqli_error($ dbc); } //關閉與數據庫的連接mysqli_close($ dbc); ?>

+0

爲什麼有一個@之前mysqli_query – jophab

+0

@是一個錯誤抑制字符 –

+0

我正在學習PHP,真的不知道這個@。我按照一些教程編寫了這段代碼。我設法按照本教程插入數據,但無法從數據庫中獲取數據。我應該刪除它嗎? – Malik

回答

1

我發現我的錯誤。我在瀏覽器中輸入了錯誤的URL。

不正確的URL:文件:/// C:/xampp/htdocs/php/view.php

正確的網址http://localhost/php/view.php

的代碼工作完全正常,並顯示我的數據從MySQL數據庫現在。

相關問題