tags:

views:

97

answers:

2

Hello,

I want the output of the code below to be "$entry does not exist" if there is no $entry value anywhere under the "site" column in the database. However, this is not happening when I enter in a value for $entry that I know is not under the "site" column in the database. Is there something wrong with my if statement?

Thanks in advance,

John

$result = mysql_query("SHOW TABLES FROM feather") 
or die(mysql_error()); 


while(list($table)= mysql_fetch_row($result))
{
  $sqlA = "SELECT COUNT(*) FROM `$table` WHERE `site` LIKE '$entry'";

  $resA = mysql_query($sqlA) or die("$sqlA:".mysql_error());
  if(mysql_num_fields($resA)>0){
  list($isThere) = mysql_fetch_row($resA);
  if ($isThere)
  {
     $table_list[] = $table;
  }
  }
  else{
print "<p class=\"topic\">$entry does not exist</p>\n";
}
}
+4  A: 

Yes. Your formatting doesn't really do it justice, though, try and clean it up so it's easier to understand. NetBeans IDE, or even Zend Development Environmont ($) do automatic code formatting, and it make life a lot easier.

$result = mysql_query("SHOW TABLES FROM feather") or die(mysql_error()); 
while(list($table)= mysql_fetch_row($result)) {
    $sqlA = "SELECT COUNT(*) FROM `$table` WHERE `site` LIKE '$entry'";

    $resA = mysql_query($sqlA) or die("$sqlA:".mysql_error());
    if(mysql_num_fields($resA)>0){
        list($isThere) = mysql_fetch_row($resA);
        if ($isThere) {
            $table_list[] = $table;
        }
    } else{
        print "<p class=\"topic\">$entry does not exist</p>\n";
    }
}

In short:

mysql_num_fields($resA) is ALWAYS greater than zero, because the COUNT(*) function will always return a value, regardless (0 or 1 or 1000000, etc.)

To fix, put the else one level up, and I would use coercion to be safer. Finally, the mysql_num_fields isn't really necessary, because you know it's always 1:

$result = mysql_query("SHOW TABLES FROM feather") or die(mysql_error()); 
while(list($table)= mysql_fetch_row($result)) {
    $sqlA = "SELECT COUNT(*) FROM `$table` WHERE `site` LIKE '$entry'";
    $resA = mysql_query($sqlA) or die("$sqlA:".mysql_error());
    list($isThere) = mysql_fetch_row($resA);
    $isThere = intval($isThere);
    if ($isThere > 0) {
        $table_list[] = $table;
    } else{
        print "<p class=\"topic\">$entry does not exist</p>\n";
    }
}

And there you have it.

razzed
Hi, I appreciate the help. When I try your code, it prints "<p class=\"topic\">$entry does not exist</p>\n" multiple times when I enter in a value for $entry that does exist.
Hey, I think I'm taking the wrong approach to my problem, so I'm going to re-post.
Nice comment, buddy :)Your code could be cleaner if you used is_int() / and / or / is_null(). Because if there's one value, but it's a "NULL" value (= problem) your code transforms it to "0" via intval() and continues whereas it shouldn't. You may use the functions is_null(), is_empty() and is_int() !
Olivier Pons
I agree, Olivier, but I guess my point was that "SELECT COUNT(*) FROM ..." doesn't ever return NULL, FWICT. But is_null or is_int, etc. are correct usage in other cases.
razzed
A: 

Please, for the love of the internet, don't built an SQL query yourself. Use PDO.

Paul Tarjan