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
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?