I need an advice with a PHP/MySQL code I am writing. What I need to do is to create a temp table and then select from it. Problem is that the temp table only lasts during the query that created it so the second query in PHP is empty.
Here are both queries, I assume the solution would be to merge them into one singe query but so far nothing I tired works.
The queries below work in phpMyAdmin if executed one after another.
The temporary table query:
$sql_temp_table = "CREATE TEMPORARY TABLE IF NOT EXISTS
tbl_temp (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY) AS
(SELECT
SUM(number_active_ads) as active_ads,
MONTH(create_date) as month,
YEAR(create_date) as year,
dealer_id as dealer_id
FROM tbl_active_ads
WHERE dealer_id = '".$rs->fields['id']."'
GROUP BY year, month
ORDER BY id)";
$rs_temp_table = $db -> Execute($sql_temp_table);
The second SELECT query:
$qry_chk_active_ads = "SELECT
a.*,
t.user_dealer_id,
t.dealer_payment_for_month,
t.dealer_payment_for_year
FROM tbl_temp as a
LEFT OUTER JOIN tbl_transaction as t
ON (a.dealer_id = t.user_dealer_id
AND a.month = t.dealer_payment_for_month
AND a.year = t.dealer_payment_for_year)
ORDER BY id DESC
LIMIT 3";
$rs_chk_active_ads = $db -> Execute($qry_chk_active_ads);
UPDATE: When I do this: var_dump($rs_temp_table); I get the following output from PHP:
object(ADORecordSet_empty)#6 (6) { ["dataProvider"]=> string(5) "empty" ["databaseType"]=> bool(false) ["EOF"]=> bool(true) ["_numOfRows"]=> int(0) ["fields"]=> bool(false) ["connection"]=> bool(false) }
Maybe this is explaining why the temp_table seems to be empty in PHP?