即使明确说明,也没有选择数据库

时间:2018-01-12 19:51:27

标签: php mysql

我在脚本中运行查询,当它在mysql工作台中运行时,它运行正常。

但是,运行脚本会导致“未选择数据库”和“#39; PowerShell中的错误。我已经按照错误提示进行了操作,但您可以在我的查询中看到我明确说明了每个表的数据库,即(ambition.ambition_totals)。

我应该添加另一个约束吗?

$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));

1 个答案:

答案 0 :(得分:1)

将数据库名称指定为mysqli_connect()

的第四个参数

这是一个假设您在localhost上连接的示例:

$conn2 = mysqli_connect("localhost", "your_username", "your_password", "ambition");
如果mysqli_select_db("ambition")不是您的默认数据库,请在mysqli_prepare语句之前

ambition