查询语法在PhpMyAdmin中运行良好,但在PHP

时间:2018-03-24 23:57:58

标签: php mysql phpmyadmin insert

所以我有这个查询

$searchresult = "CREATE TEMPORARY TABLE IF NOT EXISTS mytemp
                    (
                    id INT(11) UNSIGNED,
                    stationname varchar(30),
                    stationprice float,
                    image text,
                    updated date,
                    created text,
                    paid tinyint(1),
                    url text
                    );
                    INSERT into mytemp (id, stationname, stationprice, image, updated, created, paid, url) 

                    SELECT  id, stationname1 AS stationname,stationprice1 AS stationprice,image1 AS image, updated1 AS updated, created1 AS created, active1 AS paid, url1 AS url
                    FROM fuel 
                    WHERE (stationname1 LIKE '%" . $type .  "%' OR stationinfo1 LIKE '%" . $type .  "%' OR url1 LIKE '%" . $type .  "%')
                    UNION
                    SELECT  id, stationname2 AS stationname,  stationprice2 AS stationprice, image2 AS image, updated2 AS updated, created2 AS created, active2 AS paid, url2 AS url
                    FROM fuel 
                    WHERE (stationname2 LIKE '%" . $type .  "%' OR stationinfo2 LIKE '%" . $type .  "%' OR url2 LIKE '%" . $type .  "%')
                    UNION
                    SELECT  id, stationname3 AS stationname,  stationprice3 AS stationprice,image3 AS image, updated3 AS updated, created3 AS created, active3 AS paid, url3 AS url
                    FROM fuel 
                    WHERE (stationname3 LIKE '%" . $type .  "%' OR stationinfo3 LIKE '%" . $type .  "%' OR url3 LIKE '%" . $type .  "%')
                    UNION
                    SELECT  id, stationname4 AS stationname, stationprice4 AS stationprice, image4 AS image, updated4 AS updated, created4 AS created, active4 AS paid, url4 AS url
                    FROM fuel 
                    WHERE (stationname4 LIKE '%" . $type .  "%' OR stationinfo4 LIKE '%" . $type .  "%' OR url4 LIKE '%" . $type .  "%'); 

                    SELECT mytemp.id, mytemp.stationname, mytemp.stationprice, mytemp.image, mytemp.updated, mytemp.created, mytemp.paid, themes.rating, themes.url
                    FROM mytemp
                    INNER JOIN themes ON mytemp.url=themes.url
                    ORDER BY $searchvar;";

这给了我一条错误消息==> “错误:您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以便在'INSERT into mytemp(id,stationname,stationprice,image,updated,created,paid')附近使用正确的语法12" 。

当我将它喂入phpmyadmin时:

 CREATE TEMPORARY TABLE IF NOT EXISTS mytemp
(
id INT(11) UNSIGNED,
stationname varchar(30),
stationprice float,
image text,
updated date,
created text,
paid tinyint(1),
url text
);
INSERT into mytemp

SELECT  id, stationname1 AS stationname, stationprice1 AS stationprice,image1 AS image, updated1 AS updated, created1 AS created, active1 AS paid, url1 AS url
                        FROM fuel 
                        WHERE (stationname1 LIKE '%wwo%' OR stationinfo1 LIKE '%wwo%' OR url1 LIKE '%wwo%')
                        UNION
                        SELECT  id, stationname2 AS stationname, stationprice2 AS stationprice, image2 AS image, updated2 AS updated, created2 AS created, active2 AS paid, url2 AS url
                        FROM fuel 
                        WHERE (stationname2 LIKE '%wwo%' OR stationinfo2 LIKE '%wwo%' OR url2 LIKE '%wwo%')
                        UNION
                        SELECT  id, stationname3 AS stationname, stationprice3 AS stationprice,image3 AS image, updated3 AS updated, created3 AS created, active3 AS paid, url3 AS url
                        FROM fuel 
                        WHERE (stationname3 LIKE '%wwo%' OR stationinfo3 LIKE '%wwo%' OR url3 LIKE '%wwo%')
                        UNION
                        SELECT  id, stationname4 AS stationname, stationprice4 AS stationprice, image4 AS image, updated4 AS updated, created4 AS created, active4 AS paid, url4 AS url
                        FROM fuel 
                        WHERE (stationname4 LIKE '%wwo%' OR stationinfo4 LIKE '%wwo%' OR url4 LIKE '%wwo%');

SELECT mytemp.id, mytemp.stationname, mytemp.stationprice, mytemp.image, mytemp.updated, mytemp.created, mytemp.paid, themes.rating, themes.url
FROM mytemp
INNER JOIN themes ON mytemp.url=themes.url
ORDER BY rating;

它就像一个魅力。我无法弄清楚这个问题。到目前为止,我已经尝试将变量放在引号中并使用别名表但似乎没有任何效果。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

原来是因为我在同一个字符串中使用了多个查询语句。 我目前通过分离像

这样的语句来解决它
$searchresult = "CREATE TEMPORARY TABLE IF NOT EXISTS mytemp
                        (
                        id INT(11) UNSIGNED,
                        stationname varchar(30),
                        stationprice float,
                        image text,
                        updated date,
                        created text,
                        paid tinyint(1),
                        url text
                        );";
                    $result = mysqli_query($con,$searchresult); 

                    $searchresult = "INSERT into mytemp (id, stationname, stationprice, image, updated, created, paid, url) 

                        SELECT  id, stationname1 AS stationname,stationprice1 AS stationprice,image1 AS image, updated1 AS updated, created1 AS created, active1 AS paid, url1 AS url
                        FROM fuel 
                        WHERE (stationname1 LIKE '%" . $type .  "%' OR stationinfo1 LIKE '%" . $type .  "%' OR url1 LIKE '%" . $type .  "%')
                        UNION
                        SELECT  id, stationname2 AS stationname,  stationprice2 AS stationprice, image2 AS image, updated2 AS updated, created2 AS created, active2 AS paid, url2 AS url
                        FROM fuel 
                        WHERE (stationname2 LIKE '%" . $type .  "%' OR stationinfo2 LIKE '%" . $type .  "%' OR url2 LIKE '%" . $type .  "%')
                        UNION
                        SELECT  id, stationname3 AS stationname,  stationprice3 AS stationprice,image3 AS image, updated3 AS updated, created3 AS created, active3 AS paid, url3 AS url
                        FROM fuel 
                        WHERE (stationname3 LIKE '%" . $type .  "%' OR stationinfo3 LIKE '%" . $type .  "%' OR url3 LIKE '%" . $type .  "%')
                        UNION
                        SELECT  id, stationname4 AS stationname, stationprice4 AS stationprice, image4 AS image, updated4 AS updated, created4 AS created, active4 AS paid, url4 AS url
                        FROM fuel 
                        WHERE (stationname4 LIKE '%" . $type .  "%' OR stationinfo4 LIKE '%" . $type .  "%' OR url4 LIKE '%" . $type .  "%');";
                        $result = mysqli_query($con,$searchresult);

                        $searchresult = "SELECT mytemp.id, mytemp.stationname, mytemp.stationprice, mytemp.image, mytemp.updated, mytemp.created, mytemp.paid, themes.rating, themes.url
                        FROM mytemp
                        INNER JOIN themes ON mytemp.url=themes.url
                        ORDER BY $searchvar $choice;";

基于jh1711的回复。 Ivar根据本文档http://php.net/manual/en/mysqli.quickstart.multiple-statement.php提出了使用mysqli_multi_query()的观点。 谢谢你们!!。