0

我正在通过 php、javascript 制作一个小型电子邮件应用程序,并让它大部分工作。我迷路的地方是尝试设置查询以获取已删除并发送到垃圾箱的电子邮件。当我直接查询数据库时,我编写的查询似乎工作正常,但从 PHP 运行时失败。请帮助我解决这个令人沮丧的问题,任何帮助将不胜感激。

我的电子邮件表结构如下。

TABLE messages (
    'mid' int,
    'folderfrom' int, //where message is stored for the user who sent it
    'msgfrom' text,  //user sending message
    'msgto' text, //user getting message
    'subject' text,
    'msgdate' timestamp,
    'msg' text,
    'folderto' int, //where message is stored for recieving user.
)

下面是我用于构建和执行查询的代码。我试过在一行中运行最后一个 else 条件,但它没有用。

if ($foldernum == 1)  {
    $querytorun = "SELECT ms.mid as 'id', ms.folderto as 'folder', ms.msgfrom as 'name', ms.msgto as 'email', ms.subject as 'subject', ms.msgdate as 'date', ms.msg as 'msg' FROM messages as ms WHERE ms.msgto LIKE '{$usercode}' AND ms.folderto = {$foldernum}";
} elseif ($foldernum == 2) {
    $querytorun = "SELECT ms.mid as 'id', ms.folderfrom as 'folder', ms.msgfrom as 'name', ms.msgfrom as 'email', ms.subject as 'subject', ms.msgdate as 'date', ms.msg as 'msg' FROM messages as ms WHERE ms.msgfrom LIKE '{$usercode}' AND ms.folderfrom = {$foldernum}";
} else {
    $querytorun = "CREATE TEMPORARY TABLE tempfolder('id' int,'folder' int, 'name' text,'email' text,'subject' text,'date' datetime,'msg' text)";
    $res = mysql_query($querytorun);
    $querytorun = "INSERT INTO tempfolder SELECT mst.mid as 'id', mst.folderto as 'folder', mst.msgfrom as 'name', mst.msgto as 'email', mst.subject as 'subject', mst.msgdate as 'date', mst.msg as 'msg' FROM messages as mst WHERE mst.msgto LIKE '{$usercode}' AND mst.folderto = {$foldernum}";
    $res = mysql_query($querytorun);
    $querytorun = "INSERT INTO tempfolder SELECT mst.mid as 'id', mst.folderfrom as 'folder', mst.msgto as 'name', mst.msgfrom as 'email', mst.subject as 'subject', mst.msgdate as 'date', mst.msg as 'msg' FROM messages as mst WHERE mst.msgfrom LIKE '{$usercode}' AND mst.folderfrom = {$foldernum}";
    $res = mysql_query($querytorun);
    $querytorun = "SELECT * FROM tempfolder";   
}

$res = mysql_query($querytorun);

while($rs = mysql_fetch_object($res)) {
    $arr[] = $rs;
}

echo json_encode($arr);
4

3 回答 3

1

尝试使用反引号而不是单引号。

CREATE TEMPORARY TABLE tempfolder(`id` int,`folder` int, `name` text,`email` text,`subject` text,`date` datetime,`msg` text)
于 2014-10-22T00:57:13.330 回答
0
   $querytorun="CREATE TEMPORARY TABLE tempfolder( 'mid' int,
    'folderfrom' int, //where message is stored for the user who sent it
    'msgfrom' text,  //user sending message
    'msgto' text, //user getting message
    'subject' text,
    'msgdate' timestamp,
    'msg' text
)";
mysql_query($querytorun);
if ($foldernum == 1)  {
    $querytorun = "SELECT ms.mid as 'id', ms.folderto as 'folder', ms.msgfrom as 'name', ms.msgto as 'email', ms.subject as 'subject', ms.msgdate as 'date', ms.msg as 'msg' FROM messages as ms WHERE ms.msgto LIKE '{$usercode}' AND ms.folderto = {$foldernum}";
} elseif ($foldernum == 2) {
    $querytorun = "SELECT ms.mid as 'id', ms.folderfrom as 'folder', ms.msgfrom as 'name', ms.msgfrom as 'email', ms.subject as 'subject', ms.msgdate as 'date', ms.msg as 'msg' FROM messages as ms WHERE ms.msgfrom LIKE '{$usercode}' AND ms.folderfrom = {$foldernum}";
} else {
    $querytorun = "CREATE TEMPORARY TABLE tempfolder('id' int,'folder' int, 'name' text,'email' text,'subject' text,'date' datetime,'msg' text)";
    $res = mysql_query($querytorun);
    $querytorun = "INSERT INTO tempfolder SELECT mst.mid as 'id', mst.folderto as 'folder', mst.msgfrom as 'name', mst.msgto as 'email', mst.subject as 'subject', mst.msgdate as 'date', mst.msg as 'msg' FROM messages as mst WHERE mst.msgto LIKE '{$usercode}' AND mst.folderto = {$foldernum}";
    $res = mysql_query($querytorun);
    $querytorun = "INSERT INTO tempfolder SELECT mst.mid as 'id', mst.folderfrom as 'folder', mst.msgto as 'name', mst.msgfrom as 'email', mst.subject as 'subject', mst.msgdate as 'date', mst.msg as 'msg' FROM messages as mst WHERE mst.msgfrom LIKE '{$usercode}' AND mst.folderfrom = {$foldernum}";
    $res = mysql_query($querytorun);
    $querytorun = "SELECT * FROM tempfolder";   
}

$res = mysql_query($querytorun);

while($rs = mysql_fetch_object($res)) {
    $arr[] = $rs;
}

echo json_encode($arr);
于 2014-10-22T02:43:27.547 回答
0

所以我自己想通了,我最终将它设置为一个没有临时表的查询。相反,我用两个有效的查询做了一个联合。

$querytorun = "SELECT mst.mid as 'id', mst.folderto as 'folder', mst.msgfrom as 'namefrom', mst.msgto as 'nametoo', mst.subject as 'subject', mst.msgdate as 'date', mst.msg as 'msg' FROM messages as mst WHERE mst.msgto LIKE '{$usercode}' AND mst.folderto = {$foldernum} UNION ALL SELECT mst.mid as 'id', mst.folderfrom as 'folder', mst.msgto as 'nametoo', mst.msgfrom as 'namefrom', mst.subject as 'subject', mst.msgdate as 'date', mst.msg as 'msg' FROM messages as mst WHERE mst.msgfrom LIKE '{$usercode}' AND mst.folderfrom = {$foldernum}";
于 2014-10-26T09:28:49.497 回答