我正在尝试显示用户名,电子邮件ID,但每次输出都显示为“ 1”。 在我尝试获取并显示值的任何地方,它只显示1作为输出。
数据库:
CREATE TABLE `admin` (
`id` int(11) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`created_at` datetime DEFAULT current_timestamp(),
`email` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
PHP代码:
<?php
session_start();
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
?>
<?php
require_once('config/config.php');
//Counting Member
$members = "SELECT count( * ) as id FROM admin";
$member_result = $mysqli->query($members);
$total_member = mysqli_num_rows($member_result);
//Showing Admin Name
$admin_name = "SELECT `name` FROM `admin` WHERE id='1'";
$name_result = $mysqli->query($admin_name);
$name_sh = mysqli_num_rows($name_result);
//Showing Email
$admin_email = "SELECT `email` FROM `admin` WHERE id='1'";
$email_result = $mysqli->query($admin_email);
$email_sh = mysqli_num_rows($email_result);
?>
config.php
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'test1');
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
?>
HTML代码:
<div class="account-dropdown js-dropdown">
<div class="info clearfix">
<div class="image">
<a href="#">
<img src="images/icon/avatar-01.jpg" alt="John" />
</a>
</div>
<div class="content">
<h5 class="name">
<a href="#"><?php echo $name_sh; ?></a>
</h5>
<span class="email"><?php echo $email_sh; ?></span>
</div>
</div>
它只显示1而不是值。 在此先感谢
答案 0 :(得分:0)
那是因为您正在显示mysqli_num_rows
的结果,该结果返回结果中的行数(因此始终等于1)。
使用mysqli_stmt_fetch
来获取查询结果。
答案 1 :(得分:0)
$admin_name = "SELECT `name` FROM `admin` WHERE id='1'";
$name_result = $mysqli->query($admin_name);
$result=mysqli->fetch_array($name_result,MYSQLI_ASSOC);