PHP mysqli子查询支持 - 没有得到预期的结果

时间:2016-03-11 15:10:03

标签: php mysql mysqli

我正在尝试检索一组尚未提交处理的记录。我有三个表连接起来以获得初始结果集,包括已经处理过的结果,然后使用像is not in (select distinct id from table_that_contains_list_of_processed_ids)这样的结构。

出于某种原因,当我在MySQL Workbench中运行此查询(逐字,在每个方向上粘贴以确保)时,我得到了预期的答案,但是当我使用以下代码在mysqli中运行它时,我得到一个空的响应。

 <?php
   $conn = new mysqli($dbsrv, $dbuser, $dbpass);
   $sql = "select muid.userid as BOSSUserUrn, muid.data as BASUserUrn, mcc.id as CompletionId, mcc.course as CourseId, FROM_UNIXTIME(mcc.timecompleted) as TimeCompleted, course.fullname as CourseFullName, course.shortname as CourseShortName from moodle.mdl_user_info_data muid join moodle.mdl_course_completions mcc on mcc.userid = muid.userid join moodle.mdl_course course on course.id = mcc.course where muid.fieldid = 18 and muid.data is not null and mcc.course != 5 and mcc.timecompleted is not null and mcc.id not in (select distinct coursecompid from mdl_bas_training_records)";
   $result = $conn->query($sql);

   $api_response = array();

   if( $result && $result->num_rows > 0 ) {
     error_log("Fetched " . $result->num_rows);
     // do the processing
   }
 %>

我认为这里有先见之明的信息是,如果我拿出最后一个条款,我会在MySQL Workbench和代码中得到结果(8,如果你想知道的话),但如果我把它放回去,我会得到4结果是MySQL Workbench(正确),但在PHP中,if语句没有解析为true(我的问题)。

我看不出任何暗示mysqli无法处理这些子查询的事情。我是否遇到变量$sql中文本长度的问题?我很沮丧!

编辑:通过确保在子查询中指定数据库来解决(实际上,通过在新的mysqli实例中指定数据库)。

0 个答案:

没有答案