Hello everyone, this mysql_deprecated works very fine and retrieves information but when i tried
moving it to PDO it displays nothing. can someone help me

thank you

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';
}
}

?>

i have tried adding the quotes but it seems not to work.

PDO is supposed to query database records or better displays " querry successful" on like the working mysql deprecated counter part. I think where i have problem is in sql statement

$sql = "SELECT * FROM friends WHERE username='$user' OR friend='$user' AND status = 'accepted'";

as variable [B]$user[/B] is used for username and friend simultaneously

and here also** while($row = mysql_fetch_assoc($result))** during PDO conversion

my question again is what is the equivalent of this in PDO
while($row = mysql_fetch_assoc($result))

as mysql_fetch_array($result) =>

$result->fetch()) in PDO

thank you

Resolved. Thank you. I used ? instead of name parameters. again i quoted accepted like you said in the sql statement. whaw everything works

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.