我在xampp中创建了一个数据库,制作了一个非常简单的php脚本来访问/连接到xampp中的mysql(将其保存到xampp/htdocs)。连接正常,但是当我将其运行到浏览器时,只出现了我在php中创建的表,但mysql中的数据没有出现。可能是什么问题?
这是 PHP 代码:
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM example")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td></tr>";
}
echo "</table>";
?>
最佳答案
如果您刚刚开始使用 PHP 和 MySQL,为什么不开始学习 PDO,因为 mysql_*
函数已被弃用。
确保 example
表中有数据,请使用 PHPMyAdmin 检查并按照 this nice guide 操作.
您的代码应该是:
<?php
$db = new PDO('mysql:host=localhost;dbname=test;charset=UTF-8', 'username', 'password', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";
$stmt = $db->query("SELECT * FROM example");
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td></tr>";
}
echo "</table>";
?>
关于php - 为什么我的xampp中的mysql内容无法使用php脚本显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12668879/