2012-11-06 42 views
0

我目前与mysql_connect()一起工作,即使它的使用是不鼓励的。我的网络服务不支持PDO。我目前已将我的代码格式化为PDO,但现在我已被迫将其更改为mysяl_connect()。现在发生这些变化后,我收到一个页面错误,没有显示任何内容我包含一个database_connect.php,它与另一个页面一起工作,而不是我的改编代码。我的代码中导致此错误的问题是什么?这里是我的PAGEMysql Connect错误:根本没有显示任何内容

代码

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>" 
+0

你应该做的事情,比如'$ PDO =新PDO($ DSN,$用户,$密码);'建立连接,让你的PDO对象与 – Serg

+0

工作你们是不是才达到mysqli的查询? – kabuto178

回答

1

$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中制定的标准,并且你的主机应该至少有php 5.2.17可用,如果不是当前稳定的5.3。

现在,如果你知道PDO,你可能想尝试Mysqli而不是ext/mysql。它更像PDO并支持准备好的统计数据,不支持。你的主人应该至少有这样的。如果他们不再强调改变托管服务提供商的话。

+0

更不用说,即使'$ query-> execute()'实际上做了任何事情,你都会调用它两次,这是不必要的。 – Mansfield

0

你应该使用这样的:

$query = "SELECT class_name, class_caption, class_credit_hours, class_description FROM class"; 
$query = mysql_query($query); 
if (!$query) { 
    die("There is an error: " . mysql_error()); 
} 
//continue your code 
0

正确的代码应该是:

include ('./snippets/db_connect.php'); 

     $query = ("SELECT class_name, class_caption, class_credit_hours, class_description 
       FROM class       
       "); 
     //execute query 
     $result = mysql_query($query); 

    if ($result){ 

    $totalhours = 0; 

    while ($row = mysql_fetch_assoc($result)){ 
     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>" 
0

我真的不明白你是如何生成的代码,但我想你感到困惑。它看起来像你混合了几种不同的方法。你也调用execute()两次。如果你想使用PDO,我想你想拍摄这样的事情:

$query = "SELECT class_name, class_caption, class_credit_hours, class_description FROM class"; 
$stmt = $dbh->prepare($query); 
if ($stmt->execute()) { 
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
     // Do you stuff with your $row 
    } 
} 

如果您尝试使用mysql_ *(折旧和unsanitized),我认为你正在尝试做这样的事情:

$query = "SELECT class_name, class_caption, class_credit_hours, class_description FROM class"; 
$run = mysql_query($query); 
if ($run) { 
    while ($row = mysql_fetch_array(run)){ 
     // Do your stuff with your $row 
    } 
} 
相关问题