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条)

报告相同问题?

悬赏问题

  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办