No database selected, even though explictly stated : <p>I have a query running in a script, and when it's run in mysql workbench it works fine.</p>

<p>However, running the script results in 'no database selected' error in powershell. I've followed the error prompts but you can see in my query I explicitly state the database for each table, i.e. (ambition.ambition_totals).</p>

<p>Is there another constraint I should add to this?</p>

<pre><code>$stmt3 = mysqli_prepare($conn2,
             "UPDATE ambition.ambition_totals a
                INNER JOIN 
                (SELECT 
                    c.user AS UserID,
                    COUNT(*) AS dealers,
                    ROUND((al.NumberOfDealers / al.NumberOfDealerContacts) * 100 ,2)  AS percent
                FROM jfi_dealers.contact_events c
                JOIN jackson_id.users u
                ON c.user = u.id
                JOIN jfi_dealers.dealers d
                ON c.dealer_num = d.dealer_num
                LEFT JOIN (
                  SELECT user_id, COUNT(*) AS NumberOfDealerContacts,
                  SUM(CASE WHEN ( d.next_call_date + INTERVAL 7 DAY) THEN 1 ELSE 0 END) AS NumberOfDealers
                  FROM jackson_id.attr_list AS al
                  JOIN jfi_dealers.dealers AS d ON d.csr = al.data
                  WHERE al.attr_id = 14
                  GROUP BY user_id) AS al
                ON al.user_id = c.user
                GROUP BY UserID) as cu
                on cu.UserID = a.ext_id 
                SET a.dealers_contacted = cu.dealers,
                  a.percent_up_to_date = cu.percent;
                        ") or die(mysqli_error($conn2));
</code></pre>
