mysqli_num_rows()

时间:2013-08-10 09:45:31

标签: php mysqli

当我编写此代码时,我的PHP代码的一部分不起作用:

public $postcount = 10;


public function Updates($user_ids, $lastid)
{
    if ($lastid == 0) {
        $loadmore = '';
    } else {
        $loadmore = " and M.msg_id<$lastid ";
    }
    $db_conx = mysqli_connect("localhost", "root", "", "yusers");
    $sql     = "SELECT M.msg_id, M.uid_fk, M.message, M.created, U.fname, U.lname, M.uploads, M.profile_uid FROM messages M, users U  WHERE M.uid_fk=U.uid and M.uid_fk IN ($user_ids) $loadmore UNION SELECT M.msg_id, M.uid_fk, M.message, M.created, U.fname, U.lname, M.uploads, M.profile_uid FROM messages M, users U  WHERE M.uid_fk=U.uid and M.profile_uid IN ($user_ids) $loadmore ORDER msg_id desc limit 10" . $this->postcount;
    $query   = mysqli_query($db_conx, $sql);

我明白了:

'Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given  on line 26'

第26行:

 while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
     //something }

当我测试我的查询时:

if (!$query) {
printf("Error: %s\n", mysqli_error($db_conx));
exit(); }
它给了我:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'msg_id desc limit10' at line 2"

请帮帮我

4 个答案:

答案 0 :(得分:2)

我认为这个错误非常清楚。您发布了ORDER子句时出现语法错误。

参见 SELECT clause Manual, specifically the ORDER BY clause.

答案 1 :(得分:0)

您需要在查询结束时放置space,并将ORDER更改为ORDER BY

$sql = "SELECT M.msg_id, M.uid_fk, M.message, M.created, U.fname, U.lname, M.uploads, M.profile_uid FROM messages M, users U  WHERE M.uid_fk=U.uid and M.uid_fk IN ($user_ids) $loadmore UNION SELECT M.msg_id, M.uid_fk, M.message, M.created, U.fname, U.lname, M.uploads, M.profile_uid FROM messages M, users U  WHERE M.uid_fk=U.uid and M.profile_uid IN ($user_ids) $loadmore ORDER BY msg_id desc limit " .$this->postcount;

答案 2 :(得分:0)

您是否尝试过“ORDER BY”而非“ORDER”?

答案 3 :(得分:0)

如果可以的话,我想给你两个提示。

首先,在将它们放入代码之前总是测试sql查询,它会节省很多问题。其次,阅读关于PDO并停止使用mysqli :) http://php.net/manual/es/book.pdo.php这样做的原因是它更安全,更通用。

再见!