I've written a query using the SQL box in PHPMyAdmin (my tool of choice) and the query works well. I've now come to load this into a PHP page, and using the below, I'm getting zero results.
echo $_SESSION['ownerAC']; // 2
echo $_SESSION['versionNo']; // 1
if ($stmt = $con->prepare("SELECT node.id, node.line_type, (COUNT(parent.id) - 1) AS depth,
node.visible_index, node.printed_no, node.line_text, node.value_type, node.left_index,
node.right_index,
(SELECT COUNT(*) FROM bk_accounts a WHERE a.balance_and_income_line_id = node.id)
FROM bk_balance_and_income_lines AS parent
LEFT JOIN bk_balance_and_income_lines AS node ON node.left_index
BETWEEN parent.left_index AND parent.right_index
WHERE node.ownerID = ? AND node.version_no = ? AND parent.ownerID = node.ownerID AND parent.version_no = node.version_no
GROUP BY node.id ORDER BY node.left_index")) {
$stmt->bind_param('ii', $_SESSION['ownerAC'], $_SESSION['versionNo']);
$stmt->execute();
$tAccountQ = $stmt->get_result();
$tAccountList = $tAccountQ->fetch_all(MYSQLI_ASSOC); }
However, if I 'hardcode' the variables, in this case '2 and '1' into my query I get a full result set.
Is there a reason why this shouldn't work? Or am I being a bit dumb?
If I throw the following into command line or PHPMyAdmin SQL Console I get results:
SELECT node.id, node.line_type, (COUNT(parent.id) - 1) AS depth,
node.visible_index, node.printed_no, node.line_text, node.value_type, node.left_index,
node.right_index,
(SELECT COUNT(*) FROM bk_accounts a WHERE a.balance_and_income_line_id = node.id)
FROM bk_balance_and_income_lines AS parent
LEFT JOIN bk_balance_and_income_lines AS node ON node.left_index
BETWEEN parent.left_index AND parent.right_index
WHERE node.ownerID = '2' AND node.version_no = '1' AND parent.ownerID = node.ownerID AND parent.version_no = node.version_no
GROUP BY node.id ORDER BY node.left_index
Edit: it's now throwing this error:
PHP Fatal error: Uncaught mysqli_sql_exception: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'contract_contractr.node.line_type' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by in /home/coop/pages/chartOfAccounts.php:12
Stack trace:
#0 /home/coop/pages/chartOfAccounts.php(12): mysqli->prepare('SELECT node.id,...')
#1 /home/coop/coxxxxxxxxxxx.com/index.php(140): include('/home/coop...')
#2 {main}
thrown in /home/coop/pages/chartOfAccounts.php on line 12
Which i'm more confused by, as i'm not getting the same error (or any error, i'm just getting results) when in PHPMyAdmin, or when the ? and ? are replaced with '2' and '1'