dongxun3424 2013-06-15 00:04
浏览 31
已采纳

使用会话ID从mySQL显示特定数据

I'm trying to display the users first name after they login. They login using their email and password. Though, I would like to collect their first name and display it. Their name is in the same table that the email/password are in. Traditionally, I would use a session ID like this below.

<?php
session_start(); 
$_SESSION['first'] =  $first; 
?>

But this typically is for submitted data in a form and is used after the form has been authenticated. My question is, how would I gather data from the mySQl table rather than collecting it from the form and be able to have it has a session ID?... if that makes sense

  • 写回答

2条回答 默认 最新

  • douwei7976 2013-06-27 23:32
    关注

    Select the first name (and any other fields you desire) when checking the user name and password match.

    if (!isset($_SESSION)) session_start();
    
    $sql=sprintf("SELECT email, firstname FROM users WHERE email=%s AND password=%s", 
                  $_POST["email"], $_POST["password"]);
    
    //You'd better use parameterized query
    
    $result = mysqli->query($sql);
    
    $row = $result->fetch_assoc();
    
    if(mysqli->num_rows > 0)
    {
        $_SESSION["email"] = $row["email"];
        $_SESSION["firstname"] = $row["firstname"]; 
    }
    else 
    {
     //operation for no matches
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了