Jump to content

How to ECHO SELECT(*) COUNT VALUE?


BaconBeast321

Recommended Posts

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..

Link to comment
Share on other sites

[!--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 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')");

}




?>


Link to comment
Share on other sites

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:
[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!
Link to comment
Share on other sites

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! :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.