I am trying to optimize the sql query. Whenever I try to create temporary sql table the output gives error

here is the query code:

$k_new = mysql_result(mysql_query("CREATE TEMPORARY TABLE IF NOT EXISTS temp1 AS SELECT
        MAX(mail.id) AS id 
    FROM
        `mail` 
    WHERE
        mail.`unlink` != '$user[id]' 
        AND (
            mail.`id_kont` = '$user[id]'
        ) 
    GROUP BY
        IF(mail.`id_user` = '$user[id]',
        mail.`id_kont`,
        mail.`id_user`) 
    ORDER BY
        NULL;

-- This index is required for optimal temp tables performance

ALTER TABLE
  `temp1`
ADD
  INDEX `temp1_idx_id` (`id`);

SELECT
        COUNT(*) 
    FROM
        `mail` AS m,
        temp1 AS max 
    WHERE
        m.id = max.id 
        AND m.read = '0' 
        AND (
            m.id_kont = '$user[id]'
        ) 
        AND m.unlink != '$user[id]'"), 0);

I expect it to be the number of mails, but actual output is: Warning: mysql_result() expects parameter 1 to be resource, boolean given in /var/www/domains/shorena.ru/style/themes/vk/head.php on line 333

0 Answers