0
$FSQL = $pdo->query('SELECT * FROM `connections` WHERE `uid`="'.$my_id.'" && `type`="1" ORDER by `id` DESC');
$myfriends = '`uid`="'.$my_id.'" ';
while($po = $FSQL->fetch(PDO::FETCH_ASSOC)){
    $myfriends .= ' || `uid`="'.$po['cid'].'"';
}

$dsk = $pdo->query("SELECT * FROM `posts` WHERE ".$myfriends." ORDER by `id` DESC LIMIT ".$limitCount);

我一直在尝试创建一个不错的帖子流,我终于把我的代码写下来了。但是如果你有大量的连接(连接来自朋友、页面或事件),它似乎效率很低。

有人可以告诉我是否有更好的方法来做到这一点?

--顺便说一句:这已经很完美了,但我觉得我会遇到问题

4

2 回答 2

0

$FSQL = $pdo->query('SELECT * FROM连接WHEREuid="'.$my_id.'" &&类型="1" ORDER byidDESC');

这很容易受到SQL 注入的影响。您应该使用参数和准备好的语句。请参阅文档

工作示例

$sql = $pdo->prepare('SELECT * FROM `table` WHERE `uid`=:uid');
// Create the SQL statement, with the parameter prefixed by a ":".
$userID = "username";
// Grab the value you wish to bind to your parameter.
$sql->bindParam(':uid', $userID);
// Bind the values, using the bindParam method.
$sql->execute();
// Execute the statement with the parameters bound to the SQL query.
于 2013-03-07T19:21:44.137 回答
0

你不想使用子查询?像这样的东西...

$dsk = $pdo->query(
    "SELECT *
       FROM `posts` 
      WHERE uid IN (
            SELECT cid
              FROM `connections` 
             WHERE `uid`="'.$my_id.'" && `type`="1"
          ) 
      ORDER BY `id` DESC LIMIT " . $limitCount);

*并且在不需要所有字段时尽量不要使用。

于 2013-03-07T19:24:47.693 回答