duanpu6319 2015-01-03 02:03
浏览 24
已采纳

抓取查询,放入$ _Session

Trying to get the value I'm setting at the end for $_SESSION to be the user_id from the query, and not $username. I can't seem to work my way around modifying the query. I'm sure this is crazy easy for some of the gurus here.

if(isset($_POST['login']))
{
    $username = mysqli_real_escape_string($conn,$_POST['username']);
    $pass = mysqli_real_escape_string($conn,$_POST['password']);
    $sel_user = "select user_id, user_first_name from users where username='$username' AND password='$pass'";
    $run_user = mysqli_query($conn, $sel_user);
    $check_user = mysqli_num_rows($run_user);

    if($check_user>0){
        $_SESSION['session_id']=$username;
    }
}
  • 写回答

2条回答 默认 最新

  • dongnai3960 2015-01-03 02:14
    关注

    You need to read the row from the query results:

    $row = mysqli_fetch_assoc($run_user);
    

    Then you get the user_id from there:

    if ($row) {
        $_SESSION['session_id'] = $row['user_id'];
    }
    

    You don't need to call mysqli_num_rows(). If there were no matches, mysqli_fetch_assoc() will return false.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化