Hi
I am a php/mysql newbie ...
I want to get the number of items(number people registered for some event) in different table columns so if they are more than a a certain number X, no more registrations are possible.
I tried that using the following methods given me in another forum:
------------
SELECT COUNT(*)
FROM itmc_anmeldungen
WHERE s1t1 <> '';
To retrieve the count of rows where s1t1 is not null:
SELECT COUNT(*)
FROM itmc_anmeldungen
WHERE s1t1 IS NOT NULL;
And you can combine them:
SELECT COUNT(*)
FROM itmc_anmeldungen
WHERE s1t1 IS NOT NULL AND <> '';
------------------------
... with the syntax below:
$abfrage = "SELECT COUNT(*) FROM itmc_anmeldungen WHERE s1t1 <> ''";
$bingo = mysql_query($abfrage);
echo "<b>$bingo</b>";
For some reason, the first 2 give the result: "Resource id #4"
and the third in exactly the same format, ie with "WHERE s1t1 IS NOT NULL AND <> ''"
does not output anything.
In the database, there are currently 3 "s1t1" items, which I expect as the result.
I suppose there is something wrong with my query ...
What should I do differently?
As said, I know very basic Mysql/php, so I request for some specific answers. (I have got a good number of suggestions from different php forums, but either they dont give the expected answer OR, I do not know how to use the suggested queries to get to the result)
OR ... is there any other or better way of getting the result?
Thanks & regards
Getting the number of items in a database table column
Moderator: General Moderators
- EverLearning
- Forum Contributor
- Posts: 282
- Joined: Sat Feb 23, 2008 3:49 am
- Location: Niš, Serbia
Re: Getting the number of items in a database table column
As stated in php manual:
Read
http://php.net/mysql_query
and
http://www.php.net/manual/en/function.m ... ch-row.php
for more information.
Also
is not valid, WHERE clause is wrong, the column name is missing for the second expression. Correct code is
For SELECT statement syntax read:
http://dev.mysql.com/doc/refman/5.0/en/select.html
Google around for tutorials for PHP and MySql.
Good luck
You need to use result of the mysql_query() as an argument for mysql_fetch_row|() or some other mysql_fetch_* functionFor SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
Read
http://php.net/mysql_query
and
http://www.php.net/manual/en/function.m ... ch-row.php
for more information.
Also
Code: Select all
SELECT COUNT(*)
FROM itmc_anmeldungen
WHERE s1t1 IS NOT NULL AND <> '';Code: Select all
SELECT COUNT(*)
FROM itmc_anmeldungen
WHERE s1t1 IS NOT NULL AND s1t1 <> '';http://dev.mysql.com/doc/refman/5.0/en/select.html
Google around for tutorials for PHP and MySql.
Good luck
Re: Getting the number of items in a database table column
Hi
I am very grateful for your response and very helpful explanations.
I used this to get the results from that column:

I am very grateful for your response and very helpful explanations.
I used this to get the results from that column:
Regards$abfrage1 = "SELECT COUNT(*) as total FROM itmc_anmeldungen WHERE s1t1 <> ''";
$bingo = mysql_query($abfrage1);
while ($row = mysql_fetch_array($bingo))
{
echo $row['total'];
}
Re: Getting the number of items in a database table column
I too get the resource id#4 when performing my query. I've tried many things and can't seem to get the results back so I can use them.
<?php
function confirm_query($type_set) {
if (!$type_set) {
die("Database query failed: " . mysql_error());
}
}
function get_types() {
global $connection;
$query = "SELECT type
FROM types";
$type_set = mysql_query($query, $connection);
confirm_query($type_set);
return $type_set;
}
$type_set = get_types();
echo $type_set;
?>
What am I missing?
<?php
function confirm_query($type_set) {
if (!$type_set) {
die("Database query failed: " . mysql_error());
}
}
function get_types() {
global $connection;
$query = "SELECT type
FROM types";
$type_set = mysql_query($query, $connection);
confirm_query($type_set);
return $type_set;
}
$type_set = get_types();
echo $type_set;
?>
What am I missing?
Re: Getting the number of items in a database table column
Exactly what the other user was missing.ECJughead wrote:What am I missing?
All you are doing right now is doing the query, now you need to get the data the query selected.
mysql_fetch_row();
mysql_fetch_assoc();
Take a look at the examples too.
Re: Getting the number of items in a database table column
I understand now! Thanks!