简体   繁体   English

MySQL查询从PHP代码返回null,但在终端中工作正常

[英]MySQL query returns null from PHP code but works fine in terminal

When I run this code in PHP, the result is null, but when I run it in mysql terminal or phpmyadmin, I get what I want. 当我在PHP中运行此代码时,结果为null,但是当我在mysql终端或phpmyadmin中运行它时,得到的是我想要的。

PHP PHP

if ($_GET["action"] == "list") {
    //Get records from database
    $mainQuery = mysql_query("
    SET SQL_BIG_SELECTS=1;
    SELECT
      ci.id AS item_id,
      ar.title, ar.introtext, 
      flo.value AS logo, fph.value AS phone, fad.value AS address, fur.value AS url, fse.value AS services, fma.value AS map,
      ar.id AS joomla_id, ci.hidden_id, ci.type
    FROM kd9fb_content ar
      RIGHT JOIN calc_settings cs ON ar.catid = cs.joomla_cat  
      LEFT JOIN kd9fb_fieldsattach_values flo ON flo.articleid = ar.id AND flo.fieldsid = 1
      LEFT JOIN kd9fb_fieldsattach_values fph ON fph.articleid = ar.id AND fph.fieldsid = 2
      LEFT JOIN kd9fb_fieldsattach_values fad ON fad.articleid = ar.id AND fad.fieldsid = 3
      LEFT JOIN kd9fb_fieldsattach_values fur ON fur.articleid = ar.id AND fur.fieldsid = 4
      LEFT JOIN kd9fb_fieldsattach_values fse ON fse.articleid = ar.id AND fse.fieldsid = 5
      LEFT JOIN kd9fb_fieldsattach_values fma ON fma.articleid = ar.id AND fma.fieldsid = 6
      LEFT JOIN calc_item ci ON ci.joomla_id = ar.id
      ORDER BY ci.id DESC;
    ", $con);

    while ($row = mysql_fetch_array($mainQuery)) {      
        $rows[] = $row;
    }
    //Return result to jTable
    $jTableResult = array();
    $jTableResult['Result'] = "OK";
    $jTableResult['Records'] = $rows;
    print json_encode($jTableResult);
}

This returns NULL: 这将返回NULL:

$row = mysql_fetch_array($mainQuery)

MySQL base is ok, the code connects to base by this: MySQL库可以,代码由此连接到库:

$con = mysql_connect($host, $user, $password) or die("DB login failed!");
mysql_select_db($db, $con) or die("select failed");
mysql_query("SET NAMES utf8");

A similar code, but with other query work fine, I tested it, apparently the case in the request. 类似的代码,但在其他查询工作正常的情况下,我对其进行了测试,显然是在请求中。 By the way, I think that it is built entirely non-optimal way, but I'm not good at SQL and PHP. 顺便说一下,我认为它是完全非最佳方式构建的,但是我不擅长SQL和PHP。

So what's the problem and where I went wrong? 那是什么问题,我哪里出了问题?

Your query actually has two queries in it. 您的查询实际上有两个查询。

"SET SQL_BIG_SELECTS=1;
SELECT
  ci.id AS item_id,
  ar.title, ar.introtext, ....."

Remove the first line of the query and it should be okay. 删除查询的第一行,应该可以。 This is because the mysql_query() method only supports one query. 这是因为mysql_query()方法仅支持一个查询。 But I think this restriction is removed in the terminal IIRC. 但我认为此限制已在终端IIRC中删除。

If you need the first statement simply perform two query methods: 如果您需要第一条语句,只需执行两种查询方法:

mysql_query("SET SQL_BIG_SELECTS=1");
$query = "..."; // Rest of your query here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM