Php数据库无法检索

时间:2017-03-21 13:04:15

标签: php mysqli

我遇到了无法从数据库中检索数据的问题。它只回显我输入的句子而不是数据库中的数据。我已经尝试了好几次但它仍然无法正常工作。我的代码有什么问题吗?请帮助

<!doctype html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>database connections</title>
    </head>
    <body>
      <?php
      $username = "database-username";
      $password = "database-password";
      $host = "localhost";

      $connector = mysqli_connect($host,$username,$password)
          or die("Unable to connect");
        echo "Connections are made successfully::";
      $selected = mysqli_select_db($connector, "test_db")
        or die("Unable to connect");

      //execute the SQL query and return records
      $result = mysqli_query("SELECT * FROM table_one ");
      ?>
      <table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
      <thead>
        <tr>
          <th>Employee_id</th>
          <th>Employee_Name</th>
          <th>Employee_dob</th>
          <th>Employee_Adress</th>
          <th>Employee_dept</th>
          <td>Employee_salary</td>
        </tr>
      </thead>
      <tbody>
        <?php
          while( $row = mysqli_fetch_assoc( $result ) ){
            echo
            "<tr>
              <td>{$row\['employee_id'\]}</td>
              <td>{$row\['employee_name'\]}</td>
              <td>{$row\['employee_dob'\]}</td>
              <td>{$row\['employee_addr'\]}</td>
              <td>{$row\['employee_dept'\]}</td>
              <td>{$row\['employee_sal'\]}</td> 
            </tr>\n";
          }
        ?>
      </tbody>
    </table>
     <?php mysqli_close($connector); ?>
    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

您需要在以下代码中进行更改:

1)更改这些行:

$result = mysqli_query("SELECT * FROM table_one ");

到以下行:

$result = mysqli_query($connector, "SELECT * FROM table_one ");  

2)更改这些行:

          <td>{$row\['employee_id'\]}</td>
          <td>{$row\['employee_name'\]}</td>
          <td>{$row\['employee_dob'\]}</td>
          <td>{$row\['employee_addr'\]}</td>
          <td>{$row\['employee_dept'\]}</td>
          <td>{$row\['employee_sal'\]}</td> 

到以下行:

          <td>{$row['employee_id']}</td>
          <td>{$row['employee_name']}</td>
          <td>{$row['employee_dob']}</td>
          <td>{$row['employee_addr']}</td>
          <td>{$row['employee_dept']}</td>
          <td>{$row['employee_sal']}</td> 

就是这样。