![](/img/trans.png)
[英]Apparently there's an SQL Syntax error in this query when sent from PHP, but it runs fine on PHPMyAdmin
[英]Query Syntax Working Fine in PhpMyAdmin but Not in PHP
所以我有這個查詢
$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 ,更新,創建,付款”在第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;
它就像一個魅力。 我不知道問題所在。 到目前為止,我已經嘗試將變量放在引號中並使用別名表,但是似乎沒有任何效果。 謝謝你的幫助。
原來是因為我在同一字符串中使用了多個查詢語句。 我目前通過將如下語句分開來解決它
$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()的觀點。 多謝你們!!。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.