選擇數據從MySQL數據庫
SELECT語句用來從一個或多個表中選擇數據:
SELECT column_name(s)
FROM table_name
或者我們可以使用*字符來選擇表中的所有列:
SELECT *
FROM table_name
要了解更多有關SQL,請訪問我們的SQL教程 。
選擇數據用的MySQLi
以下示例選擇的ID,從MyGuests表名字和姓氏列,並將其顯示在頁面上:
實施例(MySQLi Object-oriented)
<?php
$servername = "localhost";
$username = "username";
$password =
"password";
$dbname =
"myDB";
// Create connection
$conn = new mysqli($servername,
$username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result
= $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " .
$row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"].
"<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
運行示例» 代碼行以從上面的例子解釋:
首先,我們設置一個選擇的ID,從MyGuests表名字和姓氏列的SQL查詢。 的下一行代碼運行查詢,並將所得數據置於一個變量名為$結果。
然後,將函數num_rows()檢查是否有多於零行返回。
如果有超過零行返回,功能fetch_assoc()將所有結果到一個關聯數組,我們可以依次通過。 的while()的循環遍歷結果集和從ID,名字和姓氏列輸出該數據。
下面的示例示出了與上述相同的例子,在庫MySQLi程序的方法:
實施例(MySQLi Procedural)
<?php
$servername = "localhost";
$username = "username";
$password =
"password";
$dbname =
"myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password,
$dbname);
// Check connection
if (!$conn) {
die("Connection
failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result
= mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row =
mysqli_fetch_assoc($result))
{
echo "id: " .
$row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"].
"<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
運行示例» 你也可以把結果以HTML表格:
實施例(MySQLi Object-oriented)
<?php
$servername = "localhost";
$username = "username";
$password =
"password";
$dbname =
"myDB";
// Create connection
$conn = new mysqli($servername,
$username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result
= $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
//
output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr><td>" .$row["id"]."</td><td>".$row["firstname"]."
".$row["lastname"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
運行示例» 選擇數據隨著PDO (+ Prepared Statements)
下面的示例使用準備語句。
它選擇的ID,從MyGuests表名字和姓氏列,並將其顯示在一個HTML表:
實施例(PDO)
<?php
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width:150px;border:1px solid black;'>" .
parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function
endChildren() {
echo "</tr>"
. "\n";
}
}
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname",
$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$stmt =
$conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as
$k=>$v) {
echo $v;
}
}
catch(PDOException
$e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>
運行示例»