2

当我在 PHP 中运行此代码时,结果为空,但是当我在 mysql 终端或 phpmyadmin 中运行它时,我得到了我想要的。

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);
}

这将返回 NULL:

$row = mysql_fetch_array($mainQuery)

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");

类似的代码,但其他查询工作正常,我对其进行了测试,显然是请求中的情况。顺便说一句,我认为它是完全非最佳方式构建的,但我不擅长 SQL 和 PHP。

那么问题出在哪里,我哪里出错了?

4

1 回答 1

4

您的查询实际上有两个查询。

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

删除查询的第一行应该没问题。这是因为 mysql_query() 方法只支持一个查询。但我认为这个限制在终端 IIRC 中被删除了。

如果您需要第一条语句,只需执行两种查询方法:

mysql_query("SET SQL_BIG_SELECTS=1");
$query = "..."; // Rest of your query here
于 2013-09-25T03:41:25.533 回答