Page 1 of 1

mySql_num_rows() Problem

Posted: Fri Oct 06, 2006 12:29 pm
by jeva39
The page show the results of a search page. I try to use this code:

Code: Select all

<?php
     $vTema=$_POST['txtTema'];
     $vRitmo=$_POST['txtRitmo'];
     $vAutor=$_POST['txtAutor'];
     $vInter=$_POST['txtInter'];
    ?>

...............more code.............

$sql = 'select id,clase,tema,ritmo,autor,arreglo,fecha,kar,nuevo,archivo from temas where RITMO like \'%'.$vRitmo.'%\' && TEMA like \'%'.$vTema.'%\' && AUTOR like \'%'.$vAutor.'%\' && ARREGLO like \'%'.$vInter.'%\' order by tema';
      
$result = mysql_query($sql);
$reg = mysql_num_rows($sql); //Line 74

if (!$reg)
  echo "<center><b><font color='#FF0000'>No existen Registros</font></b></center><br>";
Really I have two problems:

1- This warning

PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Inetpub\wwwroot\SML_PHP\total.php on line 74

2- No matter if exist or not records always appear the "No existen Registros"

If I don't use mySql_num_rows() all work fine....

Please what is wrong?

Thanks

Posted: Fri Oct 06, 2006 12:36 pm
by impulse()
I think that fault is with the MySQL statement.

Try, as a test, doing:

Code: Select all

$sql = mysql_query("SELECT * FROM <table>");
$reg = mysql_num_rows($sql);
echo "There are ", $reg, "rows.";
I'd suggest tidying your statements up. It looks like you've been coding drunk :)

Posted: Fri Oct 06, 2006 12:42 pm
by danharibo
Yeah use the Mysql_query result to get numrows :p

Posted: Fri Oct 06, 2006 12:45 pm
by RobertGonzalez
No, the problem is that you are using a string instead of a result resource in your numrows call.

It should look something like this:

Code: Select all

<?php
$result = mysql_query($sql) or die(mysql_error());
$rowcount = mysql_num_rows($result);
?>

Posted: Fri Oct 06, 2006 12:46 pm
by danharibo
Everah wrote:
It should look something like this:

Code: Select all

<?php
$result = mysql_query($sql) or die(mysql_error());
$rowcount = mysql_num_rows($result);
?>
Thats what i said! :P

Posted: Fri Oct 06, 2006 12:48 pm
by n00b Saibot
adding some error checking to mysql statements usually helps much...
e.g.

Code: Select all

$reg = mysql_num_rows($sql) or die('<b>MySQL Error</b>: '.MySQL_Error());
also use AND instead of && in your query.

edit | too late :|

Posted: Fri Oct 06, 2006 1:09 pm
by jeva39
WOW!!!! This is what I call a very fast and great help!!! With this code all work fine:

Code: Select all

$result = mysql_query($sql) or die(mysql_error());
$reg = mysql_num_rows($result);

if (!$reg)
   echo "<center><b><font color='#FF0000'>LAMENTABLEMENTE NO TENEMOS LOS TEMAS BUSCADOS.</font></b></center><br>";
Thanks very much!!!

Posted: Fri Oct 06, 2006 1:12 pm
by RobertGonzalez
danharibo wrote:
Everah wrote:
It should look something like this:

Code: Select all

<?php
$result = mysql_query($sql) or die(mysql_error());
$rowcount = mysql_num_rows($result);
?>
Thats what i said! :P
Sorry, I didn't see your code :wink:

Posted: Fri Oct 06, 2006 8:01 pm
by jeva39
Exactly! Again, thanks to all because now all working OK!!!