PHP:如何从数据库中获取数据?

时间:2018-07-23 08:28:53

标签: php sql

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

<!DOCTYPE html>
<html>
<head>
    <title>Update</title>
</head>
<body>

<table border="2">
    <tr>
        <th>Username</th>
        <th>Email</th>
        <th>Edit</th>
    </tr>
<?php
     $conn=mysqli_connect("localhost","root","","telephasic");
     $q2="select * from register where id = '".$_SESSION['id']."'";
     $run=mysqli_query($conn, $q2);
     while($row=mysqli_fetch_array($run))
     {
         $name=$row[1];
         $email=$row[2];
     ?>

    <tr>
        <td><?php echo $name; ?></td>
        <td><?php echo $email; ?></td>
        <td><a href="edit.php"> Edit </a></td>
    </tr>
 <?php } ?>
 </table> 
 </body>

在这里,我想获取存储在数据库中的单个行信息,因此无法获取数据,因此我在这里需要借助会话ID来获取代码

2 个答案:

答案 0 :(得分:1)

<?php
session_start();
$id = $_SESSION['id'];
?>
<!DOCTYPE html>
<html>
<head>
    <title>Update</title>
</head>
<body>

<table border="2">
    <tr>
        <th>Username</th>
        <th>Email</th>
        <th>Edit</th>
    </tr>
<?php
     $conn = mysqli_connect("localhost","root","","telephasic");
     $q2 = "select * from register where id = $id ";
     $run = mysqli_query($conn, $q2);
     $row = mysqli_fetch_array($run);
?>
    <tr>
        <td><?= $row['name']; ?></td>
        <td><?= $row['email']; ?></td>
        <td><a href="edit.php"> Edit </a></td>
    </tr>
 </table> 
 </body>

跳过while部分,因为您正在获取单个记录。

答案 1 :(得分:0)

您在代码第三行遇到问题

%%d

您将$ _SESSION ['id']设置为字符串“ id”,因此您不会从数据库中获取任何信息,因为您没有id =“ id”的行。删除此行,一切都会正常