Mysql:
Select COUNT (*) from Main_index;
But this here is a grammatical error.
The first method:
Check the documentation to:
Aggregate functions (AVG (), MIN (), MAX (), SUM ()) in column list clause are supported. Arguments to aggregate functions can be either plain attributes or arbitrary expressions. COUNT (*) is implicitly supported as with using GROUP by would add @count column to result set. Explicit support might is added in the future. COUNT (DISTINCT attr) is supported. Currently there can be in most one COUNT (DISTINCT) per query and a argument needs to is an attribute. Both Current restrictions on COUNT (DISTINCT) might is lifted in the future.
That is, COUNT (*) can only be used when group by, such as:
Copy Code code as follows:
Select 1 as Dummy,count (*) c from Main_index group by dummy;
+------+--------+-------+--------+
| ID | Weight | Dummy | @count |
+------+--------+-------+--------+
| 1001 | 1 | 1 | 15659 |
+------+--------+-------+--------+
The second method
Copy Code code as follows:
SELECT * from Main_index limit 0;
Show Meta;
+---------------+-------+
| variable_name | Value |
+---------------+-------+
| Total | 67 |
| Total_found | 67 |
| Time | 0.001 |
| Keyword[0] | Ha |
| Docs[0] | 67 |
| Hits[0] | 115 |
+---------------+-------+
That means using show meta to get the Total_found, which is the total number of records.
Here's a look at the show meta:
Show META shows additional meta-information about the latest query such as query time and keyword statistics:
That is, it shows some of the information that was appended to the most recent query, such as query time, keyword statistics, total records, and so on.
Copy Code code as follows:
Mysql> SELECT * from Test1 WHERE MATCH (' test|one|two ');
+------+--------+----------+------------+
| ID | Weight | group_id | date_added |
+------+--------+----------+------------+
| 1 | 3563 | 456 | 1231721236 |
| 2 | 2563 | 123 | 1231721236 |
| 4 | 1480 | 2 | 1231721236 |
+------+--------+----------+------------+
3 rows in Set (0.01 sec)
Mysql> show META;
+---------------+-------+
| variable_name | Value |
+---------------+-------+
| Total | 3 |
| Total_found | 3 |
| Time | 0.005 |
| Keyword[0] | Test |
| Docs[0] | 3 |
| Hits[0] | 5 |
| KEYWORD[1] | One |
| DOCS[1] | 1 |
| HITS[1] | 2 |
| KEYWORD[2] | Two |
| DOCS[2] | 1 |
| HITS[2] | 2 |
+---------------+-------+
Rows in Set (0.00 sec)
How do I call in PHP?
Copy Code code as follows:
<?php
Get the total number of records
Private Function Gettotalfound ($conn) {
$sql = "show meta";
$total _result = @mysql_query ($sql, $conn);
$totals = Array ();
while (($row = MYSQL_FETCH_ASSOC ($total _result))!== false) {
$totals [$row [' variable_name ']] = $row [' Value '];
}
return $totals;
}
?>
Note: If the code uses multiple database connections, the corresponding conn must be passed in, otherwise the result will not be obtained.