doufang8965 2016-02-25 22:51
浏览 17
已采纳

获取数组的最佳方法是什么

Alright, so I believe that there is a better way that I can fetch an array from the database, here's the code right now that I have.

$id = 1;
$userquery = mysql_query("SELECT * FROM login WHERE id='$id'");

while($row = mysql_fetch_array($userquery, MYSQL_ASSOC)) {
    $username = $row['username'];
    $password = $row['password'];
    $email = $row['email'];
}
  • 写回答

2条回答 默认 最新

  • dongpo2458 2016-02-26 08:06
    关注

    So If I am not wrong, you want a better way to get all the returned rows from mysql in a single statement, instead of using the while loop.

    If thats the case, then I must say mysql_ drivers do not provide any such functionality, which means that you have to manually loop through them using foreach or while.

    BUT, since mysql_ is already depricated, you are in luck! you can actually switch to a much better and newer mysqli_ or the PDO drivers, both of which DO actually have functions to get all the returned rows.

    For mysqli_: mysqli_result::fetch_all

    For PDO : PDOStatement::fetchAll

    Eg.

    mysqli_fetch_all($result,MYSQLI_ASSOC); 
    // The second argument defines what type of array should be produced 
    // by the function. `MYSQLI_ASSOC`,`MYSQLI_NUM`,`MYSQLI_BOTH`.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python