I have this 1 line SELECT COUNT(user_id) FROM userinfo;. But it's tossing me this error "Parse error: syntax error, unexpected 'COUNT' (T_STRING)". What I'm trying to do is look at this table and count how many people I have in this table and then give me back 1 answer.
Thanks
Count
- Edited
No way to tell for sure without seeing the actual code, but sounds like you need to fix some quoting in your PHP code, perhaps?
- Edited
<!DOCTYPE html>
<html>
<head>
<title>Hello!</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "admin";
$password = "Cas";
$dbtable = "usersinfo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbtable);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
AFTER THIS LINE I START TO GET ERRORS.
SELECT COUNT(user_id) FROM usersinfo;
// $sql = 'SELECT [user_id] FROM usersinfo;';
//echo $sql;
//$results = mysqli_query($conn, $sql);
//$resultCheck = mysqli_num_rows($result);
//if ($resultCheck >0) {
// while ($row = mysqli_fetch_assoc($result)){
// echo $row['user_id'] . "<br>";
// }
//}
?>
</body>
</html>
So what you have is a raw chunk of SQL text in the middle of your PHP code, without any of the following PHP code.
(Added syntax highlighting.)
The other error is using square brackets around column names, which is a SQL Server thing not (as far as I'm aware) recognised by MySQL. The standard method for quoting names (which is only necessary if the thing being quoted is something like a SQL keyword) is to use ""
but I don't remember if MySQL uses those either. But since user_id
is not an SQL keyword it doesn't need to be quoted at all.
And with a little object chaining, I believe you could do it as...
$sql = "SELECT COUNT(user_id) FROM usersinfo";
$user_count = $conn->query($sql)->fetch_column();