Q:

No database selected, even though explictly stated

I have a query running in a script, and when it's run in mysql workbench it works fine.
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).
Is there another constraint I should add to this?
$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));

A:

Specify the database name as the fourth parameter to mysqli_connect()
Here's an example that assumes you're connecting on localhost:
$conn2 = mysqli_connect("localhost", "your_username", "your_password", "ambition");

or do mysqli_select_db("ambition") before your mysqli_prepare statement if ambition was not your default database.

