MySQLi在while循环中准备语句

时间:2014-08-15 23:30:01

标签: php mysqli

我试图在mysqli_fetch_row while循环中执行4个预准备语句。目前我的代码输出所有查询的NULL或空值。这是我第一次使用预准备语句,我正在尝试将代码从mysql查询转换为mysqli。我的代码如下:

$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 个答案:

答案 0 :(得分:2)

使用单个查询来连接所有子查询:

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