【问题标题】:Mysqli using mysql variable errormysqli使用mysql变量报错
【发布时间】:2016-02-06 03:35:04
【问题描述】:

我终于在这里创建了我的帐户。你帮助我很久了,但这次我在这里找不到答案。

我是 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

第 22 行匹配在

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

在 MySqli 上运行它的正确方法是什么?

谢谢 布鲁诺

【问题讨论】:

  • 当您复制并粘贴此 SQL 查询到您的 PHP 管理员时,它会执行吗?看来您有拼写错误,但我不知道MariaDB 是否允许:b.address, (another select) x... 此外,将 SQL 命令保持为大写被认为是一种好习惯,这样维护起来更容易
  • 确实有效。我在 phpMyAdmin 上编码。
  • 我认为问题在于将“SET @var=...”与“;”一起使用最后。
  • 我的主要猜测是; 运算符。我不使用 mariadb,所以我不知道它的局限性/怪癖...

标签: php mysql json mysqli


【解决方案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

【讨论】:

  • 请不要使用 w3schools,而是使用官方文档(例如 the PHP manual,或来自该特定数据库的手册......无论如何都点赞答案......and to justify why w3schools is bad, read this link跨度>
  • 我觉得官方文档对这个问题有点苛刻,但你说的很对,w3c 学校对事情的看法很扭曲:)
  • 我对该站点的主要问题是信息过时,以及它使用的定义过于简单......最后,有时建议的实现会提供重大安全漏洞,老开发人员往往必须稍后修复(例如从不清理输入,或者在 html 页面中删除数据库详细信息等。)
  • 这部分没问题,我刚刚从另一个 SELECT 中获得了一个字段,但主要选择在“FROM ost_ticket a”下面几行。
  • 我尝试了你所说的使用“AS”的方式,但我得到了同样的错误。 sql 代码正在运行,问题是使其与“MySqli_query”函数一起使用。我认为问题在于将“SET @var=...”与“;”一起使用最后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
相关资源
最近更新 更多