Why can't I run this query through PHP mysql->query but works fine directly using PHPMyAdmin

This is baffling me, every other query works... (simple queries though):

function get_avail_timeslots($durationmin=120) {

	$td = date('Y-m-d');

	$sql = <<< THISSQL
	
SET @stdt := '{$td}';

SELECT avail.num
      , avail.user
      , avail.start_at   as avail_start_dt_time
      , avail.end_at     as avail_end_dt_time
      , avail.on_dt      as just_date_no_time
      , avail.start_time as user_start_time
      , avail.end_time   as user_end_time
FROM (
      SELECT
            dts.num, dts.on_dt, dts.start_at, dts.end_at, uh.user, uh.start_time, uh.end_time
      FROM (
            /* generates 1000, starting at a variable date */
            SELECT
                  num + 1 as num
                , @stdt on_dt
                , DATE_ADD(@stdt, INTERVAL (slots.num * {$durationmin}) MINUTE) start_at
                , DATE_ADD(@stdt, INTERVAL ((slots.num + 1) * {$durationmin}) MINUTE) end_at
            FROM (
                  /* generates 1000 rows 0 to 999 */
                  SELECT `hundreds`.digit * 100 + `tens`.digit * 10 + `ones`.digit AS num
                  FROM (
                        SELECT 0 AS digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
                        SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
                       ) `ones`
                  CROSS JOIN (
                        SELECT 0 AS digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
                        SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
                             ) `tens`
                  CROSS JOIN (
                        SELECT 0 AS digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
                        SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
                             ) `hundreds`
                  ) `slots`
            ) `dts`
      INNER JOIN user_hours `uh` ON uh.start_time < dts.end_at AND uh.end_time > dts.start_at
      ) `avail`
LEFT JOIN user_hours_breaks `uhb` ON  uhb.start_time < avail.end_at AND uhb.end_time > avail.start_at
LEFT JOIN appointments `app` ON  app.start_time < avail.end_at AND app.end_time > avail.start_at
WHERE uhb.id IS NULL
AND app.id IS NULL
;

THISSQL;

return getrows($sql);

}

print_r(get_avail_timeslots(120));

$con=mysqli_connect($GLOBALS['mysql_host'], $GLOBALS['mysql_user'], $GLOBALS['mysql_pass'], $GLOBALS['mysql_db']);

function getrows($query) {
	global $con;
	global $f;
	$result = $con->query($query) or $f->logthis("MySQL Query Error. Query: $query. Error: " . $con->error, 'Error', 'library.php', 'SQL Error');
	$rows = array();
	while($row = $result->fetch_assoc()) $rows[] = $row;
	return $rows;	
}

Open in new window



I am getting:

;
. Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT avail.num
      , avail.user
      , avail.start_at   as avail_start_dt' at line 3
MarkProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
phpMyAdmin will split your query up into chunks that can be processes.  The 'mysqli' driver however will only process one query at a time.  "SELECT avail.num" is the start of a second query in the view of the 'mysqli' driver.  Yes, I realize that phpMyAdmin uses the same driver... but it has code to parse your SQL into the 'right size' pieces.  Everywhere you have ';' is the end of a query.
0
MarkProgrammerAuthor Commented:
Any suggestions?
0
Dave BaldwinFixer of ProblemsCommented:
Split it into two queries.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MarkProgrammerAuthor Commented:
Got it, thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.