2013-02-18 40 views
0

我試圖從數據庫中拉出幾個字段的數據並使用mysqli填充一個HTML表格。我研究這個問題的嘗試已經提出了使用mysql或似乎將mysql與mysqli混合的解決方案。我一直試圖使用API​​來查找可能工作的函數,但似乎無法讓表顯示。任何建議,將不勝感激。PHP和mysqli:來自數據庫的HTML表格

<?php 
ini_set('display_errors', 'On'); 
ini_set('display_startup_errors', 'On'); 
error_reporting(E_ALL); 

$mysqli = new mysqli("$dbhost", "$dbuser", "$dbpass", "$dbname"); 
if ($mysqli->connect_errno) { 
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
} 

if (!$stmt = $mysqli->query("SELECT aid, aname FROM actor_info")) { 
    echo "Query Failed!: (" . $mysqli->errno . ") ". $mysqli->error; 
} 

if (!$stmt->execute()) { 
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; 
} 

if (!$stmt->bind_result($aid, $aname)) { 
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; 
} 

echo "ACTOR INFORMATION"; 
echo "<table border='1' cellpadding='2' cellspacing='2'"; 
echo "<tr><td>ID</td><td>NAME</td>"; 
while ($row = mysqli_fetch_array($stmt)) { 
     echo "<tr>"; 
     echo "<td>".$aid."</td>"; 
     echo "<td>".$aname."</td>"; 
     echo "</tr>"; 
} 
else { 
    echo "No records found"; 
} 
$stmt->free(); 
$mysqli->close(); 
?> 

回答

1
while ($row = mysqli_fetch_array($stmt)) { 
    echo "<tr>"; 
    echo "<td>" . $row["aid"] . "</td>"; 
    echo "<td>" . $row["aname"] . "</td>"; 
    echo "</tr>"; 
} 
+0

謝謝!那就是訣竅。我在那裏發現了太多矛盾的信息。 – user1852050 2013-02-18 03:04:37

相關問題