Log in

View Full Version : mysql deprecated to pdo query issues



mutago
10-08-2013, 03:50 PM
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 friends 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';
}
}

?>

mutago
10-08-2013, 03:58 PM
the table is users not friends

traq
10-08-2013, 04:41 PM
the table is users not friends

Is that your solution? Or do you still have a problem?

If it's still not working, please explain a little more - what result are you getting, and how is it wrong? what errors are you getting from PHP? from MySQL?

mutago
10-08-2013, 05:46 PM
the problem still remain. I think there is a mis conversion somewhere.
The PDO code is not working. i need help please

mutago
10-08-2013, 05:52 PM
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 $user is used for username and friend simultaneously

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

mutago
10-08-2013, 07:19 PM
Resolved. Thank you. I used ? instead on name parameters. again i quoted accepted in the sql statement

traq
10-08-2013, 07:22 PM
what about errors? Do you have error reporting enabled? Once you enable error reporting, are you getting errors, or is everything running fine and you're just getting an unexpected result?

This:
while ($row = $result->fetch())
{
echo 'data retrieved successfully';
}
suggests that—since you're not seeing that message—your query was successful and simply matched no rows.

whoops, didn't see your reply before I posted mine. Glad you solved it!

If your question has been answered, please mark your thread "resolved":
On your original post (post #1), click [edit], then click [go advanced]. In the "thread prefix" box, select "Resolved". Click [save changes].