2012-11-06 3 views
0

사용하지 않을 경우에도 현재 mysql_connect()으로 작업 중입니다. 내 웹 서비스는 PDO을 지원하지 않습니다. 현재 내 코드는 PDO으로 포맷되었지만 이제는이를 mysяl_connect()으로 변경해야합니다. 이 변경 후 이제는 페이지 오류가 발생하고 아무 것도 표시되지 않습니다. 내 적응 코드가 아닌 다른 페이지에서 작동하는 database_connect.php을 포함합니다. 이 오류의 원인이되는 내 코드의 문제점은 무엇입니까? 다음은 문자열 jsut 내 PAGEMySQL 연결 오류 : 전혀 표시되지 않음

코드

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, $ 사용자, $ 암호) 같은 것을해야한다, 당신은 Mysqli 쿼리를 실현하려하려고'연결을 설정하고 – Serg

+0

와 함께 작동하도록 PDO 개체를 가지고 있습니까? – 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에서 표준으로 만들어졌고, 호스트는 5.3의 현재 stable이 아니라면 php 5.2.17을 사용할 수 있어야한다.

이제 PDO를 알고있는 경우 ext/mysql 대신 Mysqli을 시도 할 수 있습니다. 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 
    } 
} 
관련 문제