dtfpznrbn503027700 2018-05-04 01:34
浏览 47
已采纳

从数据库中检索行并存储到会话数组[重复]

This question already has an answer here:

I am trying to retrieve rows from a database and store them to an array/ session array (I am a bit lost). The database I am currently retrieving from has 3 rows.

<?php
session_start();
$user_year = $_SESSION['year'];
$floor = $_SESSION['year_floor'];

include "config.php";
$query = "select * from timetable where '$user_year' = year;
$array = array();

while($row = $query->fetch_assoc()){
  $array[] = $row;

}

  echo '<script>console.log(\"$array\")</script>';
  /* close connection */
  // debug:
  print_r($array); // show all array data
  echo $array[0]['username']; // print the first rows username
  $mysqli->close();
?>

This is what I have pieced together so far, is this close? Any pointers in the right direction would be great thanks.

</div>
  • 写回答

1条回答 默认 最新

  • dongxin5429 2018-05-04 01:49
    关注

    It's close but not quite. Check the examples at

    http://php.net/manual/en/mysqli.query.php and

    http://php.net/manual/en/mysqli-result.fetch-assoc.php and

    http://php.net/manual/en/mysqli-result.fetch-all.php

    Edits below.

    I would also consider looking up Binding variables instead of injecting session variables into a query. A malicious user could potentially set your $user_year variable and edit the query. More here: http://php.net/manual/en/mysqli-stmt.bind-param.php

    <?php
    session_start();
    $user_year = $_SESSION['year'];
    $floor = $_SESSION['year_floor'];
    
    include "config.php"; //I assume you create a $mysqli object here
    $query = "select * from timetable where year = '$user_year'";
    $results_array = array(); // I try to avoid using variable types as names so have renamed to results_array
    
    $result = $mysqli->query($query); // mysqli runs the query (see man above)
    
    //The result runs the fetch_assoc (see man above)
    while($row = $result->fetch_assoc()){
         $result_array[] = $row;
    }
    
    // If you know you have a small result set you could replace the while() loop above with $result_array = $result->fetch_all()
    
    // echo '<script>console.log(\"$array\")</script>'; (this wont work because the client side javascript can't understand the php variable. But you could place this line inside the loop above.)
    
    // debug:
    print_r($result_array); // show all array data
    echo $result_array[0]['username']; // print the first rows username
    $mysqli->close(); // you don't need to do this unless saving memory mid script, but it's good practice so I left it in
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧