mySql_num_rows() Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

mySql_num_rows() Problem

Post 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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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 :)
danharibo
Forum Commoner
Posts: 76
Joined: Thu Aug 17, 2006 8:56 am

Post by danharibo »

Yeah use the Mysql_query result to get numrows :p
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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);
?>
danharibo
Forum Commoner
Posts: 76
Joined: Thu Aug 17, 2006 8:56 am

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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 :|
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post 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!!!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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:
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post by jeva39 »

Exactly! Again, thanks to all because now all working OK!!!
Post Reply