我正在尝试打印数据库中的所有行,并说“学生”#39;我正在尝试使用while循环的许多代码,甚至来自w3school的示例。但是,它没有用。这是一个简单的php代码
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn) {
die('Could not connect to MySql'.mysql_error());
}
mysql_select_db("StudDatabase") or die(mysql_error());
$sql = "SELECT * FROM Student";
mysql_query($sql);
mysql_close($conn);
?>
我在wamp server 2.2工作
答案 0 :(得分:1)
您的代码不会打印查询结果 尝试添加此代码:
$results = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($results)){
foreach($row as $cname => $cvalue){
print "$cname: $cvalue\t";
}
print "\r\n";
}
但您应该考虑使用PDO,mylsql
is deprecated:http://php.net/manual/en/pdo.connections.php
$database = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
foreach($database->query('SELECT * FROM `Student`') as $row) {
print_r($row);
}