On my site I have a script that searches through a table called products and then if mysql_num_rows = 0 , it says nothings there etc. but when I try to run the script I get the rror message -

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/virtual/site94/fst/var/www/html/pages/test.php on line 14

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/virtual/site94/fst/var/www/html/pages/test.php on line 15
In the table personal there are rows in the personal table
And here they are...

and heres my script

<?php
 $connection=mysql_connect("localhost","","");


if (!$connection) {
 echo "Could not connect to MySQL server!";
 exit;
}


$sql="SELECT * FROM products";
    $mysql_result=mysql_query($sql,$connection);
    $num_rows =mysql_num_rows($mysql_result);
    while ($row = mysql_fetch_row( $mysql_result))
    { $rec_str.="<TR>"; 
      foreach ($row as $field) $rec_str .= "<TD>$field</TD>";
    }
   echo "<B>In the table personal there are $num_rows rows in the personal table<BR>And here they are...</B><BR>";
echo $rec_str;
    mysql_close($connection);

?>

the thing is I know this script works because I tried it on a free server and it works fine , do you think this is a problem with the configuration of my hosts PHP, or is there a simpler explination?
thanks

    try this

     $mysql_result=mysql_query($sql);

      I tried that, but it just gave the same error 🙁

        thanks anyway

        Try this.

        $sql = mysql_query("SELECT * FROM products");
               $num_rows = mysql_num_rows($sql);
        while ($row = mysql_fetch_row($sql)) {
           // etc

        Cgraz

          Parse error: parse error, unexpected $ in /home/virtual/site94/fst/var/www/html/pages/tet.php on line 21

          thanks what I get when I tried your code, I looked through the code now and can't find why that error appears

            Your original code is fine.

            You probably don't actually have a table called products on your server. Or else the user you are connecting as does not have access to it.

            Try
            $result = mysql_query('Select * from products');
            if (mysql_errno()) {
            echo mysql_error();
            exit;
            }

              there is a table called products but when I tried what you said I got the error

              'No Database Selected'

              so I added mysql_select_db etc to the script and it just gave me my error message that I set for

              $db=mysql_select_db("icsm_org",$connection);
              if (!$db) {
              echo "Could not connect to the database!";
              exit;
              }

              could it be that my connection is

              $connection=mysql_connect("localhost","",""); ?

                I set the right database and my mysql username and pass but still get this message

                Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/virtual/site94/fst/var/www/html/pages/search.php on line 14

                  aha I'm getting somewhere lol

                  I tried the test script I've been using on the forum and its showing me the records in my product table

                  <?php
                   $connection=mysql_connect("localhost","icsm","------");
                  
                   if (!$connection) {
                   echo "Could not connect to MySQL server!";
                   exit;
                  }
                  
                   $db=mysql_select_db("icsm_org",$connection);
                    if (!$db) {
                   echo "Could not connect to the database!";
                   exit;
                   }
                  $sql="SELECT * FROM products";
                      $mysql_result=mysql_query($sql,$connection);
                      $num_rows =mysql_num_rows($mysql_result);
                      while ($row = mysql_fetch_row( $mysql_result))
                      { $rec_str.="<TR>"; 
                        foreach ($row as $field) $rec_str .= "<TD>$field</TD>";
                      }
                     echo "<B>In the table personal there are $num_rows rows in the personal table<BR>And here they are...</B><BR>";
                  echo $rec_str;
                      mysql_close($connection);
                  
                  ?>
                  

                  that works but this is the real script which gives errors

                  I'll try and not use the num rows command and see what happens

                  
                  <?php
                  $connection=mysql_connect("localhost","icsm","----");
                  
                  if (!$connection) {
                   echo "Sorry the database isn't working at the moment, please return later.!";
                   exit;
                  }
                  
                   $db=mysql_select_db("icsm_org",$connection);
                  
                    if (!$db) {
                   echo "Could not connect to the database!";
                   exit;
                   }
                  
                  
                   $sql="SELECT * FROM products WHERE name,company LIKE '%$name%'";
                  
                  $mysql_result=mysql_query($sql,$connection);
                  
                   $num_rows=mysql_num_rows($mysql_result);
                  
                   if  ( $num_rows == "0" ) {
                  
                   echo "Sorry no products to display";
                  
                   } else {
                  
                  echo " 
                  
                  <table>
                  
                      <tr> 
                  
                        <td valign=\"top\" align=\"center\"><b><font size=\"2\" face=\"Verdana\">Picture</font></b></td>
                  
                        <td valign=\"top\" align=\"center\">
                  
                        	<b><font size=\"2\" face=\"Verdana\">Name</font></b></div>
                  
                        </td>
                  
                        <td align=\"center\">
                  
                        	<b><font size=\"2\" face=\"Verdana\">Price</font></b></div>
                  
                        </td>
                  
                        <td align=\"center\">
                  
                        	<b><font size=\"2\" face=\"Verdana\">Manufacture</font></b></div>
                  
                        </td>
                  
                        <td align=\"center\">
                  
                        	<b><font size=\"2\" face=\"Verdana\">Description</font></b></div>
                  
                        </td>
                  
                        <td align=\"center\">
                  
                        	<b><font size=\"2\" face=\"Verdana\">Buy</font></b></div>
                  
                        </td>
                  
                      </tr>
                  
                  ";
                  
                  while ($row=mysql_fetch_array($mysql_result))
                  
                  {
                  
                  $name=$row["name"];
                  
                  $price=$row["price"];
                  
                  $company=$row["company"];
                  
                  $desc=$row["desc"];
                  
                  $picture=$row["picture"];
                  
                  
                  
                  echo "
                        	<tr>
                  
                  	<td width=180 align=\"center\">
                  
                  	<A HREF=\"$picture\"><font face=Verdana size=2>Here</A></td>
                  
                   	<td width=183 align=\"center\"><font face=Verdana size=2>$name</font></td>
                  
                   	<td width=166 align=\"center\"><font face=Verdana size=2>£$price</td>
                  
                   	<td width=121 align=\"center\"> <font face=Verdana size=2>$company</td>
                  
                              <td width=121 align=\"center\"> <font face=Verdana size=2>$desc</td>
                  
                              <td width=121 align=\"center\"> <font face=Verdana size=2><BR>
                  
                  
                  
                  <form name=\"_xclick\" target=\"paypal\" action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">
                  
                  <input type=\"hidden\" name=\"cmd\" value=\"_cart\">
                  
                  <input type=\"hidden\" name=\"business\" value=\"orders15@postmaster.co.uk\">
                  
                  <input type=\"hidden\" name=\"item_name\" value=\"$name\">
                  
                  <input type=\"hidden\" name=\"amount\" value=\"$price\">
                  
                  <input type=\"hidden\" name=\"no_note\" value=\"1\">
                  
                  <input type=\"hidden\" name=\"currency_code\" value=\"GBP\">
                  
                  <input type=\"image\" src=\"http://members.lycos.co.uk/mgcomputers/images/buy.bmp\" border=\"0\" name=\"submit\" alt=\"Make payments with PayPal - it's fast, free and secure!\">
                  
                  <input type=\"hidden\" name=\"add\" value=\"1\"> </form>
                  
                  </td>
                  
                  </tr>
                  
                  <tr><BR></tr>
                  
                  
                  ";
                  
                  }
                  
                  } 
                  
                  mysql_close($connection);
                  
                  
                  
                  ?>
                  
                  

                    when I took out the num rows command I still get this error

                    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/virtual/site94/fst/var/www/html/pages/search.php on line 63

                    ...never ending problems :-/

                      Again, check for a mysql_error after running the msql_query() function (code should always do this).

                      User icsm probably does not have rights to the table products.

                        I know i have rights to the table because the first test script showed me all products in the table fine

                          hmm so I add the mysql error code to the script and it says error where I have name,company like '%$name%' so I take that out and its finally working but it doesnt show any of the field contents lol it just shows the basic layout like if I search for mouse it says £ buy but doesnt say how much it is... :-/

                            mysql_error is very useful for checking for syntax errors. What happens is that mysql_query() returns null if there is a syntax problem. And then anything that uses the results will error out. It really is a good idea to check for errors after each mysql_query() especially when developing a new app.

                            In your case you have
                            $sql = "Select * from products where name,company like '%$name'"

                            See the problem? You have name,company in the where clause.

                              aha thanks people , anyone who replied the problem is connecting to the right database and conection and where I put name,company with my old free host I just need localhost then "" for user and pass etc =]

                                Good.

                                You may still have some html errors though maybe you have cleaned up the posted code a bit.

                                By the way, you are not planning on using the hidden data are you? Users could, for example, change the price field and get a real bargain.

                                  omg yeah lol , never though of that :-/ but it is through paypal so I can view the prices of the products they are buying, how else could you do it and keep the informating hidden?

                                    You can't. You can never trust data coming back from a user. You really need to have the server lookup the prices.

                                    It's possible to encrypt the hidden data and make things harder for the criminal user. I would put all the hidden data into one string then encrypt it. The server would then have to un encrypt it.

                                    Even there you have to be careful since a user might be able to copy the encrypted data and use it.

                                      Write a Reply...