2013-10-08 72 views
-1

下面這段代碼工作正常使用MySQL棄用但是當我試圖棄用MySQL的PDO來查詢問題

將其轉換爲PDO,它不查詢什麼。在相當長的一段時間裏,我一直在這樣做,它似乎不工作。

有人可以幫助我

working mysql_deprecated.php 

<?php 
    require('config.php'); 
     $user = $_SESSION['log']['username']; 
     $sql = "SELECT * FROM users WHERE username='$user' OR friend='$user' AND status = 'accepted'"; 
    $result = mysql_query($sql); 
    while($row = mysql_fetch_assoc($result)) 
    { 

    echo 'fine'; 

    if ($row['friend'] != $user) { $friend = $row['friend']; } else { $friend = $row['username']; } 

    $query = "SELECT *,UNIX_TIMESTAMP() - date_created AS TimeSpent FROM post WHERE 
    (username = '$friend' OR username = '$user') ORDER BY post_id DESC LIMIT 5"; 
    $result = mysql_query($query); 

    while($row = mysql_fetch_assoc($result)) 

    { 

    echo 'data retrieved successfully'; 
    } 
    } 

    ?> 


    pdo.php 

    <?php 
    $db = new PDO (
     'mysql:host=localhost;dbname=testing', 
     'root', // username 
     '' // password 
    ); 
    $user = $_SESSION['log']['username']; 
    $sql = $db->prepare(' 
       SELECT * FROM users 
       WHERE username= ? OR friend = ? and status = accepted'); 


    $sql->bindParam(1, $user, PDO::PARAM_STR); 
    $sql->bindParam(2, $user, PDO::PARAM_STR); 
    $sql->execute(); 

    while($row = $sql->fetch()) 
    { 

    echo 'fine'; 

    if ($row['friend'] != $user) { 
    $friend = $row['friend']; 
    } else { 
    $friend = $row['username']; 
    } 


    $query = $db->prepare(' 
     SELECT *,UNIX_TIMESTAMP() - date_created AS TimeSpent FROM post WHERE 
    (username = :username OR username = :username1) ORDER BY post_id DESC LIMIT 5 

    '); 
      $query->execute(array(
       ':username' => $friend, 
           ':username1' => $user)); 
        $result = $query->fetch(); 

         while ($row = $result->fetch()) 

          { 
    echo 'data retrieved successfully'; 
    } 
    } 

    ?> 

謝謝你這麼多

+0

描述你應該能夠處理錯誤[在手冊中](http:// php。淨/手動/ EN/pdo.error-handling.php) – Yogu

回答

0

嘗試改變這一行:

$sql = $db->prepare(' 
       SELECT * FROM users 
       WHERE username= ? OR friend = ? and status = accepted'); 

到:

$sql = $db->prepare(" 
       SELECT * FROM users 
       WHERE username= ? OR friend = ? and status = 'accepted'");