简体   繁体   中英

MySQLi prepared statement within while loop

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>";
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM