将一系列值与变量一起使用

时间:2017-07-07 21:08:01

标签: php mysql

我有以下疑问;

$sel_referrals1="SELECT t1.referree AS GEN1, t2.referree as GEN2, t3.referree as GEN3, t4.referree as GEN4, t5.referree as GEN5, t6.referree as GEN6, t7.referree as GEN7, t8.referree as GEN8, t9.referree as GEN9, t10.referree as GEN10, t11.referree as GEN11
                                        FROM referrals AS t1
                                        LEFT JOIN referrals AS t2 ON t2.referrer = t1.referree
                                        LEFT JOIN referrals AS t3 ON t3.referrer = t2.referree
                                        LEFT JOIN referrals AS t4 ON t4.referrer = t3.referree
                                        LEFT JOIN referrals AS t5 ON t5.referrer = t4.referree
                                        LEFT JOIN referrals AS t6 ON t6.referrer = t5.referree
                                        LEFT JOIN referrals AS t7 ON t7.referrer = t6.referree
                                        LEFT JOIN referrals AS t8 ON t8.referrer = t7.referree
                                        LEFT JOIN referrals AS t9 ON t9.referrer = t8.referree
                                        LEFT JOIN referrals AS t10 ON t10.referrer = t9.referree
                                        LEFT JOIN referrals AS t11 ON t11.referrer = t10.referree
                                        WHERE t1.referrer = '{$_SESSION['user']}'";
$selected1 = mysqli_query($conn, $sel_referrals1);
$selected_levels= mysqli_fetch_array($selected1);

我想按照以下语句处理查询结果;

$select_level1="SELECT SUM(amount) FROM topup WHERE user_id IN('{$selected1['GEN1']}')";//The is line 80
 $selected_level1 = mysqli_query($conn,$select_level1);
 $result_level1 = mysqli_fetch_row($selected_level1);
 $level1=number_format($result_level1[0],2);

但是,我收到以下错误;

  

致命错误:无法在第80行的D:\ xampp2 \ htdocs \ airtimez \ referrals.php中使用mysqli_result类型的对象作为数组

帮我调试一下

1 个答案:

答案 0 :(得分:0)

  1. $selected1['GEN1'] $selected1是使用$selected_levels获取的结果对象,而不是获取的结果。
  2. 要获取多行,fetch需要循环while($row = mysqli_fetch_array($selected1))
  3. 您不应该将变量放入SQL查询中,这将打开SQL注入。而是使用参数化查询。
  4. 如果您只需要id来获取sum只需使用子查询。
  5. 大致是:

    $sel_referrals1="SELECT SUM(amount) as da_sum FROM topup WHERE user_id IN((SELECT t1.referree
                                            FROM referrals AS t1
                                            LEFT JOIN referrals AS t2 ON t2.referrer = t1.referree
                                            LEFT JOIN referrals AS t3 ON t3.referrer = t2.referree
                                            LEFT JOIN referrals AS t4 ON t4.referrer = t3.referree
                                            LEFT JOIN referrals AS t5 ON t5.referrer = t4.referree
                                            LEFT JOIN referrals AS t6 ON t6.referrer = t5.referree
                                            LEFT JOIN referrals AS t7 ON t7.referrer = t6.referree
                                            LEFT JOIN referrals AS t8 ON t8.referrer = t7.referree
                                            LEFT JOIN referrals AS t9 ON t9.referrer = t8.referree
                                            LEFT JOIN referrals AS t10 ON t10.referrer = t9.referree
                                            LEFT JOIN referrals AS t11 ON t11.referrer = t10.referree
                                            WHERE t1.referrer = ?))";
    $selected1 = mysqli_prepare($conn, $sel_referrals1);
    mysqli_stmt_bind_param($selected1, 'i', $_SESSION['user']);
    mysqli_stmt_execute($selected1);
    while($row = mysqli_stmt_fetch($selected1)){ 
         echo number_format($row['da_sum'], 2);
    }
    

    虽然您可能希望group之前的userid总和。