View Full Version : MySQL COUNT help ( i think)
franky500
1st December 2007, 18:06
Hey guys.
I was wondering if somebody can help me out on what i "think" is simple for most of you guys.. but for me. a whole new ball game.
ok heres what im trying to do:
I am trying to be able to retrieve a total ammount of entries in a particular table.
EG: i want to sum up the total number of colums which have "test4" as an entry to it to then be able to output to a web page as a number... so if there are 30 entries in that column and 7 of them entries are "test4" then i simply want to have "7" displayed on the php webpage.
I hope some of what i have posted makes sense.
Ian.H
1st December 2007, 18:17
You're almost there Franky...
SELECT COUNT(*) AS total FROM `TABLE_NAME` WHERE `COLUMN_NAME` = 'search_query'
Using your screenshot, something like:
SELECT COUNT(*) AS total FROM `your_table_name` WHERE `GAME_ID` = 'TC1704.......'
This will result in an array / object element (depending on your coding style) named 'total' containing the number of records found.. as you're expecting.
HTH :)
Regards,
Ian
franky500
1st December 2007, 18:24
Hey Ian, Thanks,
I'm assuming however that i am doing something completely and uterly wrong:
here is what i have in my php doc:
<?php
/* Database connection information */
$hostname = "localhost";
$username = "*****";
$password = "***";
$dbname = "*****";
SELECT COUNT(*) AS total FROM `tc_services` WHERE `GAME_ID` = 'TC1704400877836';
}
?>
which gives me the error:
Parse error: syntax error, unexpected T_STRING in test.php on line 10
I have a feeling im doing this completely wrong lol.
Any ideas?
Thanks
EDIT: i think im an idiot... Any chance you could whip something up for me ?. i think i'm trying to think to much.. dont you think? /me goes to brush up on basic PHP...
SamH
1st December 2007, 18:54
I don't do PHP, but I believe it'll be something like this..
<?php
/* Database connection information */
$hostname = "localhost";
$username = "*****";
$password = "***";
$dbname = "*****";
mysql_connect( $hostname, $username, $password );
mysql_select_db( $dbname );
$query = mysql_query( "SELECT COUNT(*) AS total FROM tc_services WHERE GAME_ID = 'TC1704400877836'" );
/* $query should contain your result */
echo $query;
mysql_close();
?>
franky500
1st December 2007, 19:10
Getting close. No longer get any errors.. but i dont get the result i was looking for.. all i get from that is:
Resource id #3
Thanks for the help guys. I'm one step closer :)
SamH
1st December 2007, 19:13
I'm assuming you changed the ***** for your user, pass and db name.. yeah? ;)
AndroidXP
1st December 2007, 19:19
I don't do PHP, but I believe it'll be something like this..
It's a long time ago I did PHP, but I think it should look like this:
<?php
/* Database connection information */
$hostname = "localhost";
$username = "*****";
$password = "***";
$dbname = "*****";
mysql_connect( $hostname, $username, $password );
mysql_select_db( $dbname );
$query = mysql_query( "SELECT COUNT(*) AS total FROM tc_services WHERE GAME_ID = 'TC1704400877836'" );
/* $query should contain your result */
$count = mysql_result($query, 0, "total");
echo $count;
mysql_free_result($query);
mysql_close();
?>
:shrug: :tilt:
As far as I know, the return value of mysql_query() is a result set, containing one or multiple rows with the columns you specified in the select. There are also other mysql_ methods to loop through multiple rows (mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object, mysql_fetch_row).
SamH
1st December 2007, 19:22
Corr.. it's beginning to look easier and quicker in ASP :p
filur
1st December 2007, 19:32
echo mysql_fetch_object(mysql_query($sql))->total;
franky500
1st December 2007, 20:32
Your all stars, That one got it Android. and i have learnt something new. Cheers for the help guys.
Can anybody recommend anywhere that i can look up for tutorials... maybe a beginners guide to php / mysql. or perhaps i should skip that chapter and look at another coding language (any suggestions?)
Thanks for all the help again guys. massively appreciated.
mcgas001
1st December 2007, 20:55
Your all stars, That one got it Android. and i have learnt something new. Cheers for the help guys.
Can anybody recommend anywhere that i can look up for tutorials... maybe a beginners guide to php / mysql. or perhaps i should skip that chapter and look at another coding language (any suggestions?)
Thanks for all the help again guys. massively appreciated.
Whats your goal ?, if its PHP and MySql i have a great PDF file that will help you :thumb:
SamH
1st December 2007, 20:59
echo mysql_fetch_object(mysql_query($sql))->total;
Shut up, you! :p
franky500
1st December 2007, 21:01
Whats your goal ?, if its PHP and MySql i have a great PDF file that will help you :thumb:
Well i currently only tend to use HTML and chuck in a php script here and there (normally found from a script site of sorts...) so yeah. PHP / Mysql would suit me nicely.
mcgas001
1st December 2007, 21:12
Well i currently only tend to use HTML and chuck in a php script here and there (normally found from a script site of sorts...) so yeah. PHP / Mysql would suit me nicely.
Check your P.M's ;)
franky500
1st December 2007, 21:19
Check your P.M's ;)
Cheers, Much appreciated
mcgas001
1st December 2007, 23:08
Cheers, Much appreciated
Np :) Hope it helps :nod:
Ian.H
2nd December 2007, 08:50
Hi franky..
Hey Ian, Thanks,
I'm assuming however that i am doing something completely and uterly wrong:
here is what i have in my php doc:
<?php
/* Database connection information */
$hostname = "localhost";
$username = "*****";
$password = "***";
$dbname = "*****";
SELECT COUNT(*) AS total FROM `tc_services` WHERE `GAME_ID` = 'TC1704400877836';
}
?>
which gives me the error:
Parse error: syntax error, unexpected T_STRING in test.php on line 10
I have a feeling im doing this completely wrong lol.
Any ideas?
Thanks
EDIT: i think im an idiot... Any chance you could whip something up for me ?. i think i'm trying to think to much.. dont you think? /me goes to brush up on basic PHP...
Looks like you have everything sorted(?) from the follow-up posts. Sorry for the vagueness, I literally just posted the SQL statement, rather than adding the PHP code for it too.
Just for future reference if you're interested, the error you got basically means that the PHP script contains what it's interpreting as a string (ie: no function calls etc) but it's not enclosed with either single or double quotes (not that just adding quotes in this case would have got you the result you were after as you've probably worked out by now :) ). PHP's error handling / reporting isn't the most intuitive.. one thing I'm not keen about on the language.. even after 7 years of using it.. you just get used to working around it :)
If you do still have any problems.. feel free to drop me a PM and I'll try n help ya :)
Regards,
Ian
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.