PHP临时表的mysql查询不起作用

时间:2014-10-22 00:41:30

标签: php mysql mysqli libmysql

我正在通过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.
)

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

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);

3 个答案:

答案 0 :(得分:1)

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

CREATE TEMPORARY TABLE tempfolder(`id` int,`folder` int, `name` text,`email` text,`subject` text,`date` datetime,`msg` text)

答案 1 :(得分: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);

答案 2 :(得分: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}";