I'm trying to retrieve a set of records that have not already been presented for processing. I have three tables being joined in order to get the initial set of results including ones that have already been processed, I then use a construct like is not in (select distinct id from table_that_contains_list_of_processed_ids)
.
For some reason, when I run this query (verbatim, copy pasted in each direction to be sure) in MySQL Workbench, I get the answers expected, but when I run it in mysqli using the following code, I get an empty response.
<?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
}
%>
I think the prescient information here is that if I take out the final clause, I get results (8, in case you're wondering) in both MySQL Workbench and in the code, but if I put it back I get 4 results in MySQL Workbench (correct) but in PHP the if
statement doesn't resolve to true (my problem).
I can see nothing that suggests mysqli can't cope with these kind of subqueries. Am I hitting an issue with the length of the text in the variable $sql
? I'm flummoxed!
Edit: solved by ensuring that the database is specified in the subquery (actually, by specifying it in the new mysqli instantiation).