943,843 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 788
  • PHP RSS
Oct 13th, 2008
0

PHP Statement ?

Expand Post »
I have a db query, which show datas from mysql database.
I want to do : ıf number of rows from query is bigger than x do something if small do another thing. Namely;
php Syntax (Toggle Plain Text)
  1. $qry = mysql_query("SELECT * FROM data_table WHERE page='$page'",$dbh);
  2. $total = mysql_num_rows($qry);
  3.  
  4. if($total>7)
  5. {
  6. something...
  7. }
  8.  
  9. else{
  10. another thing;
  11. }

but it always going to first statement whatever $total is. I echo the $total. It gives 8.
What is wrong?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gandil is offline Offline
5 posts
since Jul 2006
Oct 13th, 2008
0

Re: PHP Statement ?

The is equates to
php Syntax (Toggle Plain Text)
  1. if ($total is greater than 7) {
  2. something...
  3. } else {
  4. another thing
  5. }

So when total=8 it will select the first one.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Oct 13th, 2008
0

Re: PHP Statement ?

PHP Syntax (Toggle Plain Text)
  1. if ($total is greater than 7)
  2. {
  3. something...
  4. }
  5. else
  6. {
  7. another thing
  8. }

I am when total is 1 or 8 whatever is; it is going to same statement. Why?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gandil is offline Offline
5 posts
since Jul 2006
Oct 13th, 2008
0

Re: PHP Statement ?

Try:
php Syntax (Toggle Plain Text)
  1. if (mysql_num_rows($qry) > 7 ) {
  2. something...
  3. } else {
  4. another thing
  5. }
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Oct 13th, 2008
0

Re: PHP Statement ?

Why don't you use COUNT in sql statement instead?
php Syntax (Toggle Plain Text)
  1. $qry = mysql_query("SELECT COUNT(*) FROM data_table WHERE page='$page'",$dbh);
PoA
Reputation Points: 19
Solved Threads: 9
Posting Whiz in Training
PoA is offline Offline
234 posts
since Jul 2004
Oct 14th, 2008
0

Re: PHP Statement ?

yes , try PoA way like:
php Syntax (Toggle Plain Text)
  1. $seq="select count(*) as cnt fromdata_table WHERE page='$page'";
  2. $sres=mysql_query($seq);
  3. $srow=mysql_fetch_assoc($sres);
  4.  
  5. if($srow['cnt']>7) {
  6.  
  7. //do something}
  8. else if($srow['cnt']<=7)
  9. {
  10. //do another thing
  11. }
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Oct 22nd, 2008
0

Re: PHP Statement ?

php Syntax (Toggle Plain Text)
  1. $qry = mysql_query("SELECT * FROM data_table WHERE page='$page'",$dbh);
  2. [COLOR="Red"]while($info = mysql_fetch_array( $qry )) {
  3. $total++;
  4. }[/COLOR]
  5.  
  6. if($total>7)
  7. {
  8. something...
  9. }
  10.  
  11. else{
  12. another thing;
  13. }
Try this.
i think mysql_num_rows(); will counting all the rows in the table.


but it always going to first statement whatever $total is. I echo the $total. It gives 8.
What is wrong?[/QUOTE]
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
danishbacker is offline Offline
97 posts
since Apr 2008
Oct 22nd, 2008
0

Re: PHP Statement ?

Using the COUNT(*) query by PoA or mysql_num_rows on your query will return the same value.
jt1
Reputation Points: 13
Solved Threads: 3
Newbie Poster
jt1 is offline Offline
19 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Parse error: parse error, unexpected '['
Next Thread in PHP Forum Timeline: PHP Snippets





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC