2015-11-05 37 views
0

我終於在這裏創建了我的帳戶。你已經幫了我很長時間了,但是這次我在這裏找不到答案。Mysqli使用mysql變量錯誤

我是MySqli中的新成員,我試圖運行一個在phpMyAdmin中工作的腳本。

當我嘗試使用'mysql變量'時會出現問題。代碼和錯誤如下。

$consulta = "SET @var= (SELECT ticket_id FROM ost_ticket WHERE number = 410037); 

SELECT DISTINCT a.number, a.created, b.address, 
SELECT DISTINCT title FROM ost_ticket_thread a WHERE a.ticket_id = @var AND a.source LIKE 'API')title, 
SELECT DISTINCT body FROM ost_ticket_thread a WHERE a.ticket_id = @var AND a.source LIKE 'API')body, 
d.state, 
c.body resposta 
FROM ost_ticket a 
LEFT JOIN ost_user_email b ON b.user_id = a.user_id 
LEFT JOIN ost_ticket_thread c ON c.ticket_id = a.ticket_id 
LEFT JOIN ost_ticket_status d ON d.id = a.status_id 
WHERE a.ticket_id = @var AND c.id = (SELECT MAX(a.id) FROM ost_ticket_thread a WHERE a.ticket_id = @var) "; 


$resultado = $MySQLi->query($consulta) OR trigger_error($MySQLi->error, E_USER_ERROR); 

$cont = 0; 

while ($informacao = $resultado->fetch_object()) { 


$data[$cont] = array(
'numprot'  =>  $informacao->number, 
'email'  =>  $informacao->address, 
'assunto'  =>  $informacao->title, 
'status'  =>  $informacao->state, 
'body'  =>  $informacao->body, 
'resposta'  =>  $informacao->resposta, 
'dtcriacao'  =>  $informacao->created, 
); 

$cont++; 

} 

echo "{'lista':".json_encode($data)."}"; 

正如我所說,它在phpMyAdmin中工作正常,但我得到了mysqli查詢函數中的後續錯誤。

Fatal error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT distinct a.number,a.created,b.address, (select distinct title from ost_t' at line 3 in /home/u605553750/public_html/JSONResposta.php on line 22 

$resultado = $MySQLi->query($consulta) OR trigger_error($MySQLi->error, E_USER_ERROR); 

What's對mysqli的運行它以正確的方式行22場比賽?

感謝 布魯諾

+0

當您複製並粘貼在你的PHP管理這個SQL查詢它執行?看起來你有拼寫錯誤,但我不知道'MariaDB'是否允許:'b.address,(另一個選擇)x' ...另外,將SQL命令保持爲大寫字母,使維護更容易被認爲是好的做法 – Bonatti

+0

It確實有效。我把它編碼在phpMyAdmin上。 –

+0

我認爲問題在於使用「SET @var = ...」和「;」到底。 –

回答

1

好像你從該行忘了。

SELECT DISTINCT a.number,a.created,b.address [FROM YOURTABLE]<-add this, (... 

經過澄清,我認爲我更瞭解你的意圖,在這種情況下,你應該確保你的子查詢返回一個select_expression。注意「AS」?

SELECT DISTINCT a.number,a.created,b.address, 
(SELECT DISTINCT title FROM ost_ticket_thread a WHERE a.ticket_id = @var AND a.source LIKE 'API') AS title 

http://dev.mysql.com/doc/refman/5.7/en/select.html

+1

請代替w3schools,使用官方文檔(如[PHP手冊](http://php.net/manual/en/function.mysql-select-db.php))或手冊特定的數據庫...無論如何,在答案上大拇指.... [和證明爲什麼w3schools不好,請閱讀此鏈接](http://www.w3fools.com/) – Bonatti

+0

我覺得官方文檔有點對這個問題苛刻,但你絕對正確,w3c學校留下了一個非常傾斜的東西:) –

+1

我與該網站的主要問題是過時的信息,以及它使用的過於簡單化的定義...最後,有時候,建議實施提供了主要的安全漏洞,老開發人員往往不得不修復拉特呃(比如從不清理輸入,或者在html頁面中刪除數據庫詳細信息,等等。) – Bonatti