dongyan1491 2014-08-15 23:30
浏览 58
已采纳

MySQLi在while循环中准备语句

I'm attempting to execute 4 prepared statement within a mysqli_fetch_row while loop. Currently my code is outputting NULL or empty values for all queries. This is my first time using prepared statements and I am trying to convert my code from mysql queries to mysqli. My code is below:

$users = mysqli_query($link, "SELECT id FROM users WHERE approved = '1'");
while ($user = mysqli_fetch_row($users)) {
    if ($twitter_username_query = mysqli_prepare($username_query, "SELECT username FROM personas WHERE user_id = ?")) {
        mysqli_stmt_bind_param($username_query, "d", $user[0]);
        mysqli_stmt_execute($username_query);
        mysqli_stmt_bind_result($username_query, $username);
        mysqli_stmt_fetch($username_query);
        mysqli_stmt_close($twitter_username_query);
    }
    if ($count_user_clicks = mysqli_prepare($count_user_clicks, "SELECT count(distinct txid) FROM users_clicks WHERE user_id = ?")) {
        mysqli_stmt_bind_param($count_user_clicks, "d", $user[0]);
        mysqli_stmt_execute($count_user_clicks);
        mysqli_stmt_bind_result($count_user_clicks, $user_clicks);
        mysqli_stmt_fetch($count_user_clicks);
        mysqli_stmt_close($count_user_clicks);
    }
    if ($count_user_installs = mysqli_prepare("SELECT count(txid) FROM (SELECT txid FROM users_installs WHERE user_id = ? GROUP BY txid) table1")) {
        mysqli_stmt_bind_param($count_user_installs, "d", $user[0]);
        mysqli_stmt_execute($count_user_installs);
        mysqli_stmt_bind_result($count_user_installs, $user_installs);
        mysqli_stmt_fetch($count_user_installs);
        mysqli_stmt_close($count_user_installs);
    }
    if ($calc_user_cost = mysqli_prepare($calc_user_cost, "SELECT sum(earnings) FROM (SELECT max(cost) as earnings FROM users_clicks WHERE user_id = ? GROUP BY txid) table1")) {
        mysqli_stmt_bind_param($calc_user_cost, "d", $user[0]);
        mysqli_stmt_execute($calc_user_cost);
        mysqli_stmt_bind_result($calc_user_cost, $user_cost);
        mysqli_stmt_fetch($calc_user_cost);
        mysqli_stmt_close($calc_user_cost);
    }
    if ($user_clicks == '0') {
        $user_conv = 0;
    } else {
        $user_conv = ($user_installs / $user_clicks) * 100;
    }
    echo "<tr>";
    echo "<td><b>".$username."</b></td>";
    echo "<td>".number_format($user_clicks)."</td>";
    echo "<td>".number_format($user_installs)."</td>";
    if ($user_installs[0] == '0') {
        echo "<td>$0.00</td>";
    } else {
        echo "<td>$".number_format($user_cost / $user_installs, 2)."</td>";
    }
    echo "<td>".number_format($user_conv, 2)."%</td>";
    echo "</tr>";
}
  • 写回答

1条回答 默认 最新

  • douqiu0351 2014-08-15 23:43
    关注

    Use a single query that JOINs all the subqueries:

    SELECT u.id, p.username, IFNULL(c.clicks, 0) AS clicks, IFNULL(i.installs, 0) AS installs, IFNULL(e.earnings, 0) AS earnings
    FROM users AS u
    JOIN personas AS p ON p.user_id = u.id
    LEFT JOIN (
        SELECT user_id, COUNT(DISTINCT txid) AS clicks
        FROM user_clicks
        GROUP BY user_id) AS c ON c.user_id = u.id
    LEFT JOIN (
        SELECT user_id, COUNT(DISTINCT txid) AS installs
        FROM user_installs
        GROUP BY user_id) AS i ON i.user_id = u.id
    LEFT JOIN (
        SELECT user_id, SUM(earnings) AS earnings
        FROM (SELECT user_id, txid, MAX(cost) AS earnings
              FROM user_clicks
              GROUP BY use_id, txid) AS table1
        GROUP BY user_id) AS e
    WHERE u.approved = 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统