Click here to Skip to main content
15,389,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good evening. May I know
how to display count() sql function using php
.

What I hve tried is
<pre lang="HTML"><pre> <?php

    $sql = "SELECT count (tilam_status)
    As Tilam Status
    FROM `akuan`
    WHERE `tilam_status` = 'ada';"
    $result = mysqli_query($conn, $sql);
    $value = mysqli_fetch_assoc($result);
    echo $value;
  ?>


Howgver, it say here
Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Status FROM `akuan` WHERE `tilam_status` = 'ada'' at line 2 in C:\xampp2\htdocs\asset\rumusan.php:492 Stack trace: #0 C:\xampp2\htdocs\asset\rumusan.php(492): mysqli_query(Object(mysqli), 'SELECT count (t...') #1 {main} thrown in C:\xampp2\htdocs\asset\rumusan.php on line 492



Fatal error: Uncaught mysqli_sql_exception: FUNCTION spakkp.count does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual in C:\xampp2\htdocs\asset\rumusan.php:492 Stack trace: #0 C:\xampp2\htdocs\asset\rumusan.php(492): mysqli_query(Object(mysqli), 'SELECT count (t...') #1 {main} thrown in C:\xampp2\htdocs\asset\rumusan.php on line 492
Aset	Bilangan patut ada	Bilangan ada	Bilangan tiada
Meja belajar bersama rak


What I have tried:

May I know y? Tq for lending me your hand.
Posted
Updated 15-Jun-22 22:13pm
v3

Character positioning:
PHP
    $sql = "SELECT count (tilam_status)
...
    WHERE `tilam_status` = 'ada';"
                                ^
                                ^
    $result = mysqli_query($conn, $sql);
The ending semicolon is inside the string, rather than outside:
PHP
    $sql = "SELECT count (tilam_status)
...
    WHERE `tilam_status` = 'ada'";
                                 ^
                                 ^
    $result = mysqli_query($conn, $sql);


You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a problem like this!
   
Quote:
SQL
SELECT count (tilam_status)
As Tilam Status
FROM `akuan`
...
You've tried to give your result column a name which contains a space. That's not a valid SQL identifier.
MySQL :: MySQL 8.0 Reference Manual :: 9.2 Schema Object Names[^]

Either change the name of your result column:
SQL
As TilamStatusCount
or quote the column name:
SQL
As `Tilam Status`
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900