2016-04-13 32 views
1

我對PHP和SQL相當陌生,所以請對我輕鬆點。我將如何去迴應我在我的數據庫中創建的屬性表中的行?PHP:如何回顯數據庫中的行?

<?php 
    //CREATE CONNECTION 
    $servername = "localhost"; 
    $username = "root"; 
    $password = "root"; 
    $conn = new mysqli($servername, $username, $password); 
    // CHECK THE CONNECTION 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
    // CREATE ESTATE AGENTS DATABASE 
    $sql = "CREATE DATABASE IF NOT EXISTS estate_agents"; 
    if ($conn->query($sql) === TRUE) { 
     $dbname= "estate_agents"; 
     $newConnection = new mysqli($servername, $username, $password,$dbname); 
     //CREATE PROPERTIES TABLE 
     $sql = "CREATE TABLE IF NOT EXISTS properties (
      property_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
      property_name VARCHAR(50) NOT NULL, 
      property_cost VARCHAR(50) NOT NULL, 
      property_description VARCHAR(500) NOT NULL 
     )"; 
     if ($newConnection->query($sql) === TRUE) { 
     } 
     else { 
      echo "Error creating table: " . $newConnection->error . "</br>"; 
     } 
    } 
    else { 
     echo "estate_agents not functional: " . $newConnection->error . "</br>"; 
    } 
?> 
+0

你有沒有嘗試這樣做呢? – Mattia

+0

你需要做的第一件事是'選擇'一些行。 –

+1

你甚至在你的屬性表中有行嗎? –

回答

1
<?php 
    //CREATE CONNECTION 
    $servername = "localhost"; 
    $username = "root"; 
    $password = "root"; 
    $conn = new mysqli($servername, $username, $password); 
    // CHECK THE CONNECTION 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
    // CREATE ESTATE AGENTS DATABASE 
    $sql = "CREATE DATABASE IF NOT EXISTS estate_agents"; 
    if ($conn->query($sql) === TRUE) { 
     $dbname= "estate_agents"; 
     $newConnection = new mysqli($servername, $username, $password,$dbname); 
     //CREATE PROPERTIES TABLE 
     $sql = "CREATE TABLE IF NOT EXISTS properties (
      property_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
      property_name VARCHAR(50) NOT NULL, 
      property_cost VARCHAR(50) NOT NULL, 
      property_description VARCHAR(500) NOT NULL 
     )"; 
     if ($newConnection->query($sql) === TRUE) { 
      $query = "SELECT * FROM properties"; 
      $result = $newConnection->query($query); 
        foreach ($result as $row) { 
        $id = $row['property_id']; 
        $name = $row['property_name']; 
        $cost = $row['property_cost']; 
        $description = $row['property_description']; 

        echo $id; 
        } 
     } 
     else { 
      echo "Error creating table: " . $newConnection->error . "</br>"; 
     } 
    } 
    else { 
     echo "estate_agents not functional: " . $newConnection->error . "</br>"; 
    } 
?> 
+0

@Leon Burman這是否行得通? –

+0

完美的作品!謝謝Kurt –

+1

沒問題。如果你希望它看起來不錯,你可以製作一個表格,但要確保你的'

'標籤不在foreach循環中。 ''和'​​'標籤應該在循環中。快樂的編碼。 –

1
<?php 
//CREATE CONNECTION 
$servername = "localhost"; 
$username = "root"; 
$password = "root"; 
$conn = new mysqli($servername, $username, $password); 
// CHECK THE CONNECTION 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
$dbname= "estate_agents"; 
$newConnection = new mysqli($servername, $username, $password,$dbname); 
//CREATE PROPERTIES TABLE 
$sql = "SELECT * FROM properties"; 
$resultSet = $conn->query($sql); 
//to get the num rows, you have to mysqli_num_rows  
//you might want to create a function on your db class for num rows and return the count with return mysqli_num_rows($result); 
$rowcount=$conn->numRows($resultSet); 
// plz dont forget to create the function numRows on your class file 
相關問題