嘿,我是php / mysql的新手,我正在尝试执行一个非常简单的PHP代码,它将显示表的内容。我觉得代码是完美的,我没有得到任何错误消息,但由于某种原因它不起作用。我知道你们讨厌调试这样的问题,但是 如果你能帮我,我会很感激。这是PHP。
<?php
$conn=mysql_connect("localhost","demo","abc") or die(mysql_error());
mysql_select_db("practice");
$sql="SELECT*FROM contact";
$result=mysql_query($sql,$conn) or die(mysql_error());
while($row=mysql_fetch_assoc($result)){
foreach($row as $name => $value){
print "$name: $value <br>\n";
} //end foreach
print "<br /> \n";
} //end while
?>
答案 0 :(得分:1)
你正在使用旧的mysql库,这是一个否定的
使用Mysqli Extension轻松满足您的所有数据库访问需求。我甚至会为你重构一下。
$conn = new Mysqli('localhost', 'demo', 'abc', 'practice');
$sql = "SELECT*FROM contact";
$results = $conn->query($sql);
while($row = $results->fetch_assoc())
{
var_dump($row);
}
编辑:JimiDini发布了一个你绝对应该阅读的链接。 http://phptherightway.com/
答案 1 :(得分:0)
试试这个
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// or if you want to enable all PHP error reports, use this code below and comment out the one above
//error_reporting(-1);
$dbhost = 'localhost';// Server name (usually localhost)
$dbuser = 'user';// SQL Username (Make sure the user has access to the database!).
$dbpass = 'password';// SQL Password.
$dbase = 'db name';// SQL Database Name.
//connection to the database
$conn = mysql_connect($dbhost,$dbuser,$dbpass) or die(mysql_error());
$sql = "SELECT * FROM `contact`";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
print_r($row);
}
但您将来应该使用mysqli