3

我正在尝试整理我网站的邮件垃圾部分,这很麻烦,我之前的查询有问题,终于让它工作了,但现在分页也造成了麻烦,我收到了这个错误

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Arken\pagination\function.php on line 9
SELECT COUNT(*) as `num` FROM user_inbox WHERE user_inbox.receiver_user_id='4' AND user_inbox.mail_deleted ='1' UNION SELECT * FROM user_outbox WHERE user_outbox.sender_user_id='4' AND user_outbox.mail_deleted ='1'

The used SELECT statements have a different number of columns

第 9 行周围的代码如下所示

$query = "SELECT COUNT(*) as `num` FROM {$query}";
        $row = mysql_fetch_array(mysql_query($query)) or die($query."<br/><br/>".mysql_error());;
        $total = $row['num'];

9号线是这mysql_fetch_array条线

我的 user_inbox 表看起来像这样

+------------+---------------+------+-----+----------+
| Field             | Type          | Extra          |
+------------+---------------+------+-----+----------+
| message_id        | int(11)       | auto_increment |
| receiver_user_id  | int(11)                        |
| receiver_username | varchar(255)                   |
| sender_user_id    | int(11)                        |
| sender_username   | varchar(255)                   |
| mail_subject      | varchar(255)                   |
| mail_message      | text                           |
| mail_date_sent    | datetime                       |
| mail_viewed       | enum('0','1')                  |
| mail_deleted      | enum('0','1')                  |
+------------+---------------+------+-----+----------+

我的 user_outbox 表看起来像这样

+------------+---------------+------+-----+----------+
| Field             | Type          | Extra          |
+------------+---------------+------+-----+----------+
| message_id        | int(11)       | auto_increment |
| sender_user_id    | int(11)                        |
| sender_username   | varchar(255)                   |
| receiver_user_id  | int(11)                        |
| receiver_username | varchar(255)                   |
| mail_subject      | varchar(255)                   |
| mail_message      | text                           |
| mail_date_sent    | datetime                       |
| mail_viewed       | enum('0','1')                  |
| mail_deleted      | enum('0','1')                  |
+------------+---------------+------+-----+----------+

我真的很感谢你们能提供的任何帮助,尝试整理网站的这一部分已经将近 2 天了,我需要完成它。感谢您的任何反馈。

4

3 回答 3

3

问题是您使用 UNION 返回不同数量的列。

SELECT COUNT(*) as `num` FROM user_inbox ...
UNION
SELECT * FROM user_outbox ...

您可以让 MySQL 为您计算行数:

SELECT COUNT(*) AS `num`
FROM
(
     SELECT * FROM user_inbox ...
     UNION ALL
     SELECT * FROM user_outbox ...
) AS your_table_alias

或者:

SELECT 
    (SELECT COUNT(*) FROM user_inbox ...) +
    (SELECT COUNT(*) FROM user_outbox ...) AS `num`
于 2012-05-20T11:09:34.087 回答
3

查询的第二部分(在 UNION 之后)也应该有 COUNT(*)。

SELECT COUNT(*) as `num` FROM user_inbox WHERE user_inbox.receiver_user_id='4' AND user_inbox.mail_deleted ='1' UNION SELECT COUNT(*)  FROM user_outbox WHERE user_outbox.sender_user_id='4' AND user_outbox.mail_deleted ='1' 
于 2012-05-20T11:10:40.420 回答
0

您应该对查询“$query”进行更多研究。看起来您正在为 PHP(我认为)代码中的变量“$query”定义的查询中选择不同数量的列(至少错误说明了这一点)。

于 2012-05-20T11:11:48.093 回答