简体   繁体   English

mysql获取名称的ID

[英]mysql get the id of the name

Hello i have a Database like this: 您好我有一个这样的数据库:

ID Name Password
1  Hans  *****

Now when I login I get the PHP Session of the Name. 现在,当我登录时,我得到名称的PHP会话。

Now I want to get the ID of the I entered Name in the Login and saved in the NameSession, and save it to a SESSION too. 现在,我想获取我在登录名中输入的名称的ID并保存在NameSession中,并将其也保存到SESSION中。

I have searched in the Internet because its looks not so hard, but I havent found anything . 我之所以在互联网上搜索是因为它看上去并不那么难,但是我什么都没找到。

This is my Code so far. 到目前为止,这是我的代码。

$sql = "SELECT * FROM login WHERE Login='".$loginname."' and Password = '".$loginpassword."'";
$result = mysql_query($sql);
if (mysql_num_rows($result)>0) { //...wenn ein Datensatz mit diesem Nicknamen gefunden wurde:
    echo "Willkommen ".$loginname; //Fehlermeldung

    //Session registieren
    $_SESSION['loginname'] = $loginname;
    $_SESSION['UserID'] = $User_ID;

    //Text ausgeben
header('Location: home.php');
exit();  
}

To fetch the corresponding id of the particular record. 获取特定记录的相应id Assuming that your table structure is ID Name Password as you mentioned in your question. 假设您的表结构是您在问题中提到的ID Name Password According to which your SQL query should rather be like: 根据哪种查询,您的SQL查询应该类似于:

"SELECT * FROM login WHERE Name ='".$loginname."' and Password = '".$loginpassword."'";

Change this: 更改此:

if (mysql_num_rows($result)>0) { //...wenn ein Datensatz mit diesem Nicknamen gefunden wurde:
    echo "Willkommen ".$loginname;

to this: 对此:

if (mysql_num_rows($result)>0) { //...wenn ein Datensatz mit diesem Nicknamen gefunden wurde:
  $row = mysql_fetch_array($result);
    echo $row['ID'];

  //saving the id and username in sessions
  $_SESSION['loginname'] = $loginname;
  $_SESSION['UserID'] = $row['ID'];

Note: This is still bad practice, Kindly study mysqli or PDO . 注意:这仍然是不好的做法,请学习mysqliPDO

PHP Manual: MySQL Improved Extension PHP手册:MySQL改进的扩展

PHP Manual: PHP Data Objects PHP手册:PHP数据对象

change 更改

$_SESSION['UserID'] = $User_ID;

to

$_SESSION['UserID'] = $result['ID'];

or if you had included explode($result) after the sql query, the $ID variable would be available. 或者,如果您在sql查询之后添加了explode($result) ,则$ID变量将可用。

This is with the assumption that only 1 row has been returned from your query, you may want to perform a loop the $result array (all returned rows), depending on how far you want to go with the login workflow. 这是基于以下假设:查询仅返回了1行,因此您可能希望对$result数组(所有返回的行)执行循环,具体取决于要使用登录工作流的距离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM