-
SQL Query
Is it possible to use the below query? I can't seem to get the count for the second table. :confused:
PHP Code:
$sSQL = mysql_query("SELECT COUNT(*) FROM users, contacts");
$details = mysql_fetch_array($sSQL);
echo "The total amount of users is <b>$details[0]</b>.<br>
The total amount of contacts is <b>$details[1]</b>.<p>";
-
Re: SQL Query
That won't worrk. Firstly what your query is doing, is combining two tables users, contracters. When you do this the tables arn't appended to one another they are literally combined where ever users, contracters combination of records is returned together. So the number of records returned would be (number of records in users) * (number of records in contacters). The example below demonstrates this:
Code:
table1 table2
1.1 2.1
1.2 2.2
1.3 2.3
1.4 2.4
SELECT * FROM table1,table2
result
1.1 2.1
1.1 2.2
1.1 2.3
1.1 2.4
1.2 2.1
1.2 2.2
1.2 2.3
1.2 2.4
1.3 2.1
1.3 2.2
1.3 2.3
1.3 2.4
1.4 2.1
1.4 2.2
1.4 2.3
1.4 2.4
total records = 16 (4 * 4)
Back to your problem, there are two ways around it, you can either execute two separate queries or use SQL to return a table containing two rows. One with the number of rows from the users table an one with the number of rows from the contracters table:
Code:
(SELECT COUNT(*) FROM users) UNION ALL (SELECT COUNT(*) FROM contracters);