2017-07-25 104 views
1

我正在學習php並用「控制面板」創建一個基本站點。現在,我在嘗試獲取註冊用戶的數量時遇到了我的代碼錯誤。 我的數據庫:Php count註冊用戶

-- 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 @[email protected]@CHARACTER_SET_CLIENT */; 
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; 
/*!40101 SET @[email protected]@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', '[email protected]', '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 [email protected]_CHARACTER_SET_CLIENT */; 
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 
/*!40101 SET [email protected]_COLLATION_CONNECTION */; 

index.php文件是

<?php 
 
include dbh.inc.php; 
 
?> 
 
<html> 
 
<head> 
 
\t <title>Sidebar-Test</title> 
 
\t <link rel="stylesheet" type="text/css" href="css/style.css"> 
 
\t <link rel="stylesheet" type="text/css" href="css/flaticon.css"> 
 
</head> 
 
<body> 
 
<div id="header"> 
 
\t <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> 
 

 
\t <div class="content"> 
 
\t \t <h1>Dashboard</h1> 
 
\t \t <div id="box1" class="box"> 
 
\t \t <h3>Users</h3> 
 
\t \t <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."; 
 

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

而且dbh.inc.php:

<?php 
 

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

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

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

我試過錯誤報告,但mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);返回任何內容,但進入在phpMyAdmin select COUNT(user_id) registered_users from users將返回 registered_users 1. 任何幫助將不勝感激!

+0

那是什麼您遇到的錯誤? – RToyo

+2

'include dbh.inc.php;'should be'include'dbh.inc.php';'(它是一個字符串) –

+0

我真的希望你提供的登錄數據完全改變,而你實際上並沒有登錄到數據庫中'root'。 – GrumpyCrouton

回答

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

您選擇計數registered_users但你要讀出的一場count不存在。改爲使用$row['registered_users']

+0

我做了,感謝那個錯誤的發現,數據庫錯誤仍然存​​在。 –

0

與此更改SQL:

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

並嘗試賦值爲:

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

沒有上訴效果,謝謝你的時間,雖然! –