BaconBeast321 Posted May 29, 2006 Share Posted May 29, 2006 Hi there, I am using this:$count = mysql_query("SELECT COUNT(*) from announcements");But I don't know how to get a variable with the value of the number of record pulled from the table.I tried to echo("$count") just got resource ID#3then tried : $countz= $count[announcements];still nothing.. Quote Link to comment https://forums.phpfreaks.com/topic/10674-how-to-echo-select-count-value/ Share on other sites More sharing options...
.josh Posted May 29, 2006 Share Posted May 29, 2006 $count = mysql_query("select count(*) as blah from announcements");$num = mysql_fetch_array($count);echo $num['blah']; Quote Link to comment https://forums.phpfreaks.com/topic/10674-how-to-echo-select-count-value/#findComment-39843 Share on other sites More sharing options...
BaconBeast321 Posted May 29, 2006 Author Share Posted May 29, 2006 [!--quoteo(post=377970:date=May 28 2006, 09:49 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 28 2006, 09:49 PM) [snapback]377970[/snapback][/div][div class=\'quotemain\'][!--quotec--]$count = mysql_query("select count(*) as blah from announcements");$num = mysql_fetch_array($count);echo $num['blah'];[/quote]sweet thanks man, worked a charm! I now have one last problem, I want this select query to find out how many there are in the table then if there is zero , apply the new announcment with identifier value of 1and if 1 in table apply 2 to the value. e.t.c I have it working but, instead of just adding one value to the array it adds all 10 lol heres the script : /<?php$count = mysql_query("select count(*) as identifier from announcements");$num = mysql_fetch_array($count);$identy = $num["identifier"];echo("$identy");if($identy = ">9") {$insert10= MYSQL_QUERY("INSERT INTO announcements (id, username, announcements, date, identifier)". "VALUES ('NULL', '$username', '$newannounce', '$cdate', '11')");}if($identy = ">8") {$insert10= MYSQL_QUERY("INSERT INTO announcements (id, username, announcements, date, identifier)". "VALUES ('NULL', '$username', '$newannounce', '$cdate', '10')");}if($identy = ">7") {$insert9= MYSQL_QUERY("INSERT INTO announcements (id, username, announcements, date, identifier)". "VALUES ('NULL', '$username', '$newannounce', '$cdate', '9')");}if($identy = ">6") {$insert8= MYSQL_QUERY("INSERT INTO announcements (id, username, announcements, date, identifier)". "VALUES ('NULL', '$username', '$newannounce', '$cdate', '8')");}if($identy = ">5") {$insert7= MYSQL_QUERY("INSERT INTO announcements (id, username, announcements, date, identifier)". "VALUES ('NULL', '$username', '$newannounce', '$cdate', '7')");}if($identy = ">4") {$insert6= MYSQL_QUERY("INSERT INTO announcements (id, username, announcements, date, identifier)". "VALUES ('NULL', '$username', '$newannounce', '$cdate', '6')");}if($identy = ">3") {$insert5= MYSQL_QUERY("INSERT INTO announcements (id, username, announcements, date, identifier)". "VALUES ('NULL', '$username', '$newannounce', '$cdate', '5')");}if($identy = ">2") {$insert4= MYSQL_QUERY("INSERT INTO announcements (id, username, announcements, date, identifier)". "VALUES ('NULL', '$username', '$newannounce', '$cdate', '4')");}if($identy = ">1") {$insert3= MYSQL_QUERY("INSERT INTO announcements (id, username, announcements, date, identifier)". "VALUES ('NULL', '$username', '$newannounce', '$cdate', '3')");}if($identy = ">0") {$insert2= MYSQL_QUERY("INSERT INTO announcements (id, username, announcements, date, identifier)". "VALUES ('NULL', '$username', '$newannounce', '$cdate', '2')");}?> Quote Link to comment https://forums.phpfreaks.com/topic/10674-how-to-echo-select-count-value/#findComment-39850 Share on other sites More sharing options...
wildteen88 Posted May 29, 2006 Share Posted May 29, 2006 Are you trying to check whether $identy is equal to or greate than a certain number in your if statements? If you are you are doing it the wrong way! Inseat compering the current value of $identy with a value you are assigning a value to $identy!one equal (=) sign is the assignment operatortwo equal (==) signs is the comparision operatorNow since you are using an = sign in all your if statements they are doing to be returning true as PHP was successfull in assigning $identy with the value ">9", ">8", ">7" etc in every iuf statement you hadNow if you want to compare something you'll want to do this:[code]if($foo == $bar) { // true} else { // false}[/code]So you will want to add an extra = sign in each if statement.But I think what you are doing is seeing whether $identy is equal or greather than something? If you are then you'll want to do this:[code]if($identy >= "9") { //do something}[/code]Also you might want to look in to [a href=\"http://uk2.php.net/manual/en/control-structures.elseif.php\" target=\"_blank\"]if/elseif[/a] statements. However I belive you can do away with all the if statement and use just one query! Quote Link to comment https://forums.phpfreaks.com/topic/10674-how-to-echo-select-count-value/#findComment-39921 Share on other sites More sharing options...
AndyB Posted May 29, 2006 Share Posted May 29, 2006 Not to mention that the logic for all the queries looks very weird. Is there a reason why you need all those queries? Quote Link to comment https://forums.phpfreaks.com/topic/10674-how-to-echo-select-count-value/#findComment-39922 Share on other sites More sharing options...
BaconBeast321 Posted May 30, 2006 Author Share Posted May 30, 2006 Hey, thanks wildteen that double "==" worked a treat!!! Sorry I didnt really explain myself that well, this page is for a user for announcment privlages who can add an announcment in a form.The Info from the form is turned into a variable, and since I only want their to be 10 anonuncments max on the main page, I figured running an if statement for counting and inserting values would be the way to do it.It seems to be working now, I will put in a delete function so when count brings back 10 values, it will automatically delete the oldest colum... not really sure how to do that...Thanks also for your response AndyB, I am not sure if i need all these queries but since I am so new to PHP I couldn't figure out a cleaner method of achieiving this....I am also hoping that when I echo the announcement to a table on the main page that the data will come out ok and that I can restrict the table size so the "privalged user" doesn't have to use much html when writing the message...Thanks alot guys! :) Quote Link to comment https://forums.phpfreaks.com/topic/10674-how-to-echo-select-count-value/#findComment-40155 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.