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;

``````\$qry = mysql_query("SELECT * FROM data_table WHERE page='\$page'",\$dbh);
\$total = mysql_num_rows(\$qry);

if(\$total>7)
{
something...
}

else{
another thing;
}``````

but it always going to first statement whatever \$total is. I echo the \$total. It gives 8.
What is wrong?

The is equates to

``````if (\$total is greater than 7) {
something...
} else {
another thing
}``````

So when total=8 it will select the first one.

Try:

``````if (mysql_num_rows(\$qry) > 7 ) {
something...
} else {
another thing
}``````

## All 7 Replies

The is equates to

``````if (\$total is greater than 7) {
something...
} else {
another thing
}``````

So when total=8 it will select the first one.

``````if (\$total is greater than 7)
{
something...
}
else
{
another thing
}``````

I am when total is 1 or 8 whatever is; it is going to same statement. Why?

Try:

``````if (mysql_num_rows(\$qry) > 7 ) {
something...
} else {
another thing
}``````

Why don't you use COUNT in sql statement instead?

``\$qry = mysql_query("SELECT COUNT(*) FROM data_table WHERE page='\$page'",\$dbh);``

yes , try PoA way like:

``````\$seq="select count(*) as cnt fromdata_table WHERE page='\$page'";
\$sres=mysql_query(\$seq);
\$srow=mysql_fetch_assoc(\$sres);

if(\$srow['cnt']>7) {

//do something}
else if(\$srow['cnt']<=7)
{
//do another thing
}``````
``````\$qry = mysql_query("SELECT * FROM data_table WHERE page='\$page'",\$dbh);
while(\$info = mysql_fetch_array( \$qry )) {
\$total++;
}

if(\$total>7)
{
something...
}

else{
another thing;
}
``````

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?

Using the COUNT(*) query by PoA or mysql_num_rows on your query will return the same value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.