如何从数据库中获取所有用户数据?

时间:2017-06-03 10:24:04

标签: php database mysqli cron

我想获取所有用户数据,但我只获得一个用户详细信息,请帮我解决

$conn = mysqli_connect("localhost", "root", "", "bitmining");
$sql6="SELECT username FROM users";         
if($result = mysqli_query($conn, $sql6)){       
    while ($row=mysqli_fetch_array($result)){               
    //Hashrate Data Fetch
    $investedusername = $row['username'];

    $sql3="SELECT sum(hashrate_amount) as total FROM buyhashrate WHERE invested_username='$investedusername'";
    $result = mysqli_query($conn, $sql3);           
    $row = mysqli_fetch_assoc($result);
    //Total Value of Hashrate
    echo $row['total'] . " GH/s";               
    echo "<br />";      
}
    $result->close();
}

3 个答案:

答案 0 :(得分:0)

您重新使用$ result字段,将您的第二个引用更改为......

    $result1 = mysqli_query($conn, $sql3);           
    $row = mysqli_fetch_assoc($result1);

这将停止重置

中用于主循环的值
while ($row=mysqli_fetch_array($result)){ 

答案 1 :(得分:0)

对mysqli_query使用$ result变量  $ result = mysqli_query($ conn,$ sql3);

答案 2 :(得分:0)

虽然其他答案对你的错误是正确的,但我想给你一个更好的解决方案。

尝试使用这样的连接:

$conn = mysqli_connect("localhost", "root", "", "bitmining");
$sql="SELECT SUM(hashrate_amount) AS total FROM users AS t1 LEFT JOIN buyhashrate AS t2 ON (t1.username=t2.invested_username) GROUP BY t1.username";         
if($result = mysqli_query($conn, $sql)){       
    while ($row=mysqli_fetch_array($result)){               
        //Total Value of Hashrate
        echo $row['total'] . " GH/s";               
        echo "<br />";      
    }
    $result->close();
}

这样您只需从数据库中执行一次查询。但是使用你的方法你有n + 1个查询,其中n是用户数。因此,对于一百个用户,有101个查询。