這裏有幾個例子:
// restricted/dbconnect.php
<?php
function db(){
return new mysqli('host', 'username', 'password', 'database');
}
?>
// mysqli::fetch_object()
<?php
session_start();
if(isset($_SESSION['email'])){
include 'restricted/dbconnect.php'; $db = db();
$result = $db->query("SELECT uid FROM master WHERE emailid='{$_SESSION['email']}'");
if($result->num_rows > 0){
while($row = $result->fetch_object()){
echo "<div>column name:{$row->columnName}</div><div>other column name:{$row->otherColumnName}</div>";
}
}
else{
echo 'No results were found';
}
$result->free(); $db->close();
}
else{
header('LOCATION:otherpage.php'); die();
}
?>
// mysqli::fetch_assoc()
<?php
session_start();
if(isset($_SESSION['email'])){
include 'restricted/dbconnect.php'; $db = db();
$result = $db->query("SELECT uid FROM master WHERE emailid='{$_SESSION['email']}'");
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<div>column name:{$row['columnName']}</div><div>other column name:{$row['otherColumnName']}</div>";
}
}
else{
echo 'No results were found';
}
$result->free(); $db->close();
}
else{
header('LOCATION:otherpage.php'); die();
}
?>
// mysqli::fetch_row()
<?php
session_start();
if(isset($_SESSION['email'])){
include 'restricted/dbconnect.php'; $db = db();
$result = $db->query("SELECT uid FROM master WHERE emailid='{$_SESSION['email']}'");
if($result->num_rows > 0){
while($row = $result->fetch_row()){
echo "<div>column name:$row[0]</div><div>other column name:$row[1]</div>";
}
}
else{
echo 'No results were found';
}
$result->free(); $db->close();
}
else{
header('LOCATION:otherpage.php'); die();
}
?>
利用面向對象的方法將節省您的打字。請注意,您可以將單維數字陣列放在雙引號內,而不使用大括號。
'mysql_query','mysqli_fetch_array' - 你正在使用的功能,從不同** **擴展 – zerkms