下面的这段代码适用于不推荐使用的mysql,但是当我尝试时
将其转换为 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';
}
}
?>
太感谢了