我目前正在使用mysql_connect()
,尽管不鼓励使用它。我的 Web 服务不支持 PDO
。我目前已将代码格式化为 PDO
,但现在我被迫将其更改为 mysяl_connect()
。经过这些更改后,我现在收到页面错误并且没有任何显示。我包含一个 database_connect.php
,它正在与另一个页面一起使用,但不使用我的改编代码。我的代码中导致此错误的问题是什么?这是我的PAGE
代码
include ('./snippets/db_connect.php');
$query = ("SELECT class_name, class_caption, class_credit_hours, class_description
FROM class
");
$query->execute();
if ($query->execute()){
$totalhours = 0;
while ($row = mysql_fetch_assoc( $query )){
print "<b>" . $row['class_name'] . "</b><br>";
print $row['class_caption'] . "<br>";
print $row['class_description'] . "<br>";
print $row ['class_credit_hours'] . "hrs. <br>";
print "------------------------------<br />";
$totalhours += $row['class_credit_hours'];
}
print "<p>Total hours for Major: " . $totalhours . ".</p>";
"<div> </div>"
最佳答案
$query
只是一个字符串,因此没有 execute
方法。你永远不会调用mysql_query($query)
。所以没有结果集。
$sql = "SELECT class_name, class_caption, class_credit_hours, class_description FROM class"
$result = mysql_query($sql);
if($result) {
while ($row = mysql_fetch_assoc($result)){
/// do stuff
}
}
此外,如果您的主机不支持 PDO,您应该进行切换,即使您不打算使用它。他们没有理由不支持它,它是 php 5.1 中的标准,如果不是当前稳定的 5.3,您的主机至少应该有 php 5.2.17。
现在,如果您了解 PDO,您可能想尝试 Mysqli
而不是ext/mysql
。它更像是 PDO,支持准备好的语句等等。你的主人至少应该有这个。如果他们不这样做,那么就会加倍强调更换托管提供商。
关于php - Mysql连接错误: not showing anything at all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13252250/