Jump to content


Photo

How to ECHO SELECT(*) COUNT VALUE?


  • Please log in to reply
5 replies to this topic

#1 BaconBeast321

BaconBeast321
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 29 May 2006 - 01:36 AM

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#3
then tried : $countz= $count[announcements];

still nothing..



#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 29 May 2006 - 02:49 AM

$count = mysql_query("select count(*) as blah from announcements");

$num = mysql_fetch_array($count);

echo $num['blah'];


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 BaconBeast321

BaconBeast321
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 29 May 2006 - 03:47 AM

[!--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) View Post[/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 1
and 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')");

}




?>




#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 29 May 2006 - 12:17 PM

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 operator
two equal (==) signs is the comparision operator

Now 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 had

Now if you want to compare something you'll want to do this:
if($foo == $bar) {
    // true
} else {
    // false
}
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:
if($identy >= "9") {
    //do something
}

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!

#5 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 29 May 2006 - 12:28 PM

Not to mention that the logic for all the queries looks very weird. Is there a reason why you need all those queries?
Legend has it that reading the manual never killed anyone.
My site

#6 BaconBeast321

BaconBeast321
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 30 May 2006 - 04:03 AM

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! :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users