我有一个包含“email、senha、apelido 和 data_criacao”列的表。我使用 PHP 和 MySQL 制作了一个登录系统,现在我想显示一 strip 有用户昵称(Apelido)的消息,但我不能。我尝试了一整夜...
我尝试过获取,尝试过 while 循环...
文件:login.php
<?php
session_start();
include('conexao.php');
if(empty($_POST['email']) || empty($_POST['senha'])){
header('Location: index.php');
exit();
}
$email = mysqli_real_escape_string($conexao, $_POST['email']);
$senha = mysqli_real_escape_string($conexao, $_POST['senha']);
$query = "select apelido from cadastros where email = '{$email}' and
senha=md5('{$senha}')";
$result1 = mysqli_query($conexao, $query);
$row1 = mysqli_num_rows($result1);
if($row1 == 1){
$apelido = $row1['apelido'];
$_SESSION['usuario'] = $apelido;
header('Location: painel.php');
exit();
} else {
header('Location: index.php');
exit();
}
?>
文件:painel.php
<?php
session_start();
include('verifica_login.php');
?>
<h2><?php echo $_SESSION['usuario']; ?></h2>
<h2> <a href="logout.php">SAIR</h2>
它应该显示“apelido”,但当我提交表单时没有任何反应。如果我将 $_SESSION['usuario'] = $apelido;
更改为 $_SESSION['usuario'] = $email;
,它会正常显示电子邮件
最佳答案
您实际上并未获取查询返回的数据。 $result1
只是指向整个结果集的指针,它不包含实际的行数据。要获取数据,您必须获取它( one row at a time 或 all at once )。大多数 PHP/mysqli 教程都会向您展示这个额外的步骤。您尝试使用 mysqli_num_rows() 的结果,它只是一个显示查询返回的行数的数值,就好像它是一个包含实际行数据的数组。
在您的情况下,您似乎只期望一行,因此您可以这样做:
$result1 = mysqli_query($conexao, $query);
if ($row1 = mysqli_fetch_assoc($result1)) //if there is a row to read, this will both assign the row data into $row1 and also cause the if statement to return true
{
$apelido = $row1['apelido'];
//...etc
关于php - 使用php7从MySQL打印数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56150313/