Php计数注册用户

时间:2017-07-25 16:25:01

标签: php sql database

我正在学习php并使用“控制面板”创建一个基本站点。现在,我在尝试获取注册用户数量时遇到了代码错误。 我的数据库:

-- phpMyAdmin SQL Dump
-- version 4.2.12deb2+deb8u2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jul 25, 2017 at 06:10 PM
-- Server version: 5.5.55-0+deb8u1
-- PHP Version: 5.6.30-0+deb8u1

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `login`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) NOT NULL,
  `user_uname` varchar(256) NOT NULL,
  `user_email` varchar(256) NOT NULL,
  `user_pwd` varchar(256) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`user_id`, `user_uname`, `user_email`, `user_pwd`) VALUES(1, 'Test1', 'johndoe@mail.com', 'HashedPassword');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
 ADD PRIMARY KEY (`user_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

index.php文件是

<?php
include dbh.inc.php;
?>
<html>
<head>
	<title>Sidebar-Test</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
	<link rel="stylesheet" type="text/css" href="css/flaticon.css">
</head>
<body>
<div id="header">
	<div class="logo"><a href='#'>Side<span>Source</span></a></div>
</div>
<div id="container">

  <div class="sidebar">
    <ul id="nav">
      <li><span class="flaticon-graphic"></span><a href="#">Dashboard</a></li>
      <li><span class="flaticon-download"></span><a href="#">Download</a></li>
      <li><span class="flaticon-settings-work-tool"></span><a href="#">Settings</a></li>
    </ul>
  </div>

	<div class="content">
		<h1>Dashboard</h1>
		<div id="box1" class="box">
		<h3>Users</h3>
		<p id="Box1P">
<?php
$sql = "select COUNT(user_id) registered_users from users";
$result = mysqli_query($conn, $sql) or die ("Query error!");


while ($row = mysqli_fetch_array($result)) {

    $var = $row['registered_users'];

    echo "There are currently " .$var. " users.";

}
?>
		</div>
	</div>
	
</div>
</body>
</html>

和dbh.inc.php:

<?php

$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "login";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

if (!$conn) {
	die("Connection failed: " . mysqli_connect_error());
}

我已尝试过错误报告,但mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);没有返回任何内容,但在phpmyadmin中输入select COUNT(user_id) registered_users from users会返回 注册用户 1。 任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:3)

$sql = "select COUNT(user_id) registered_users from users";
$var = $row['count'];

您将计数选为 registered_users但您正在尝试读取不存在的字段count。请改用$row['registered_users']

答案 1 :(得分:0)

用这个改变你的SQL:

$sql = "select COUNT(user_id) from users";

并尝试将值指定为:

$var = $row['COUNT(user_id)'];