如何使用ID显示数据库中的名称? [英] How to display name from database using id?

查看:290
本文介绍了如何使用ID显示数据库中的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用登录名创建一个小帐户,现在我必须显示登录的ID的姓名.假设,当我打印他的姓名时,我的数据库中现在有两个帐户,显示ID号.我使用ID号登录时为1. 2你能告诉我发生了什么吗?我应该在哪里错了?,

i'm making a small account using login, now i have to display name from ID who is logged in. suppose, i have two accounts into my database now as i print his name, it shows ID no. 1's name while i'm logged in using ID no. 2 can you tell me what's going on? where i'm suppose to be wrong?,

这是我的change_setting_db.php:

<?php
$con=mysqli_connect("localhost","root","Bhawanku","members");
// Check connection

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM admin");
?>

这是我的general_setting.php:

<div id="change_name">
    <label><strong>Name: </strong></label>
        <?php
        include('change_setting_db.php');

        while($row = mysqli_fetch_array($result))
        {
            echo $row['first_name']." ".$row['last_name'];
        }
        ?>
        <a id="display_float" href="change_name.php">Edit</a>
    </div><hr>

已编辑

我尝试过,但是没有用..

i tried but it's not working..

<?php
$con=mysqli_connect("localhost","root","Bhawanku","members");
// Check connection

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM admin");
if ($row = mysqli_fetch_array($result)) {
    $id=$row['id'];
mysqli_query($con,"SELECT * FROM admin WHERE id='$id' ");
}
?>

推荐答案

$result = mysqli_query($con,"SELECT * FROM admin");中对数据库进行查询时,需要传递存储在会话变量中的用户ID或其他内容.

When you make the query to the database in $result = mysqli_query($con,"SELECT * FROM admin"); you need to pass the user id stored in a session variable or something.

看看这个:

$uid = $_SESSION['uid'];
$result = mysqli_query($con, "SELECT * FROM admin WHERE uid = '$uid'");

这篇关于如何使用ID显示数据库中的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆