Select Count(*) from Table of MySQL in PHP

select count(*) from table of mysql in php

You need to alias the aggregate using the as keyword in order to call it from mysql_fetch_assoc

$result=mysql_query("SELECT count(*) as total from Students");
$data=mysql_fetch_assoc($result);
echo $data['total'];

PHP MySQL Query Select Count

To count non-null and non-empty records, you can do:

SELECT COUNT(*)
FROM Employee
WHERE UserName IS NOT NULL AND UserName != ''

You don't need to use TRIM(UserName), because trailing spaces are ignored when comparing strings.

The full PHP code should be like this:

$query = "SELECT COUNT(*)
FROM Employee
WHERE UserName IS NOT NULL AND UserName != ''";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($Count);
$stmt->fetch();
echo "Count: $Count";

php mysql select count from 2 tables

this could be done in 1 query:

SELECT item.itemNO, item.type, item.name,
COUNT(*) AS totalRemarks
FROM item
LEFT JOIN remark ON remark.itemNO = item.itemNO
GROUP BY item.itemNO, item.type, item.name
ORDER BY RAND()
LIMIT 5

Left join, as you probably want to see items with no remarks as well

I notice a flaw: in case of no remarks, it will still mention "1"

this change could solve it:

SELECT item.itemNO, item.type, item.name,
COUNT(remark.itemNO) AS totalRemarks
FROM item
LEFT JOIN remark ON remark.itemNO = item.itemNO
GROUP BY item.itemNO, item.type, item.name
ORDER BY RAND()
LIMIT 5

SELECT Count php/sql

Is first letter in table name is really capital. Please check it first.

or Try :

    $query = "SELECT COUNT(*) as totalno FROM Status where name_x=".$id;
$result = mysql_query($query);
while($data=mysql_fetch_array($result)){
$count = $data['totalno'];
}
echo $count;

MySQL SELECT COUNT(*) From two tables based on multiple conditions

This is you're probably looking for:

SELECT COUNT(C.id) AS [count]
FROM t_contacts C
INNER JOIN t_contacts_meta M ON M.cid = C.id
AND M.interest IN ('interest_1', 'interest_2', 'interest_n')
WHERE C.state = 'state_value'
AND C.profession = 'profession_value'
AND C.sex = 'sex_value'

Hope this will help you.

SELECT count rows from 2 tables and sum the results

Well, I would suggest you to do it like

  select ( select count(*) from Table1 ) + ( select count(*) from Table2 ) 
as total_rows

executing this query and getting the value of total_rows will return you true result

Or you can create a stored procedure to do the same thing. as explained below

CREATE PROCEDURE sp_Test
AS
-- Create two integer values
DECLARE @tableOneCount int, @tableTwoCount int

-- Get the number of rows from the first table
SELECT @tableOneCount = (SELECT COUNT(*) FROM Table1
WHERE WhereClause)
SELECT @tableTwoCount = (SELECT COUNT(*) FROM Table2
WHERE WhereClause)

-- Return the sum of the two table sizes
SELECT TotalCount = @tableOneCount + @tableTwoCount

Select count(column) from table of mysql in php

I hope you have the connection done. Here something is column which you are using to filter the output as desired.

$result="select count(mediaID) from media where something="1"";
echo" the sentence".$result"."


Related Topics



Leave a reply



Submit