i get this error . i just started php and i'm following a tutorial on databases..

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /customers/rootworks.be/rootworks.be/httpd.www/insert.php on line 18
Database Output

MAIN PAGE CODE

$username="removed";
$password=" censored.. hehe 🙂 ";
$database="removed";

$first=$POST['first'];
$last=$
POST['last'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( " kan geen verbinding maken met database ");
$insert = "INSERT INTO contacts VALUES ('','$first','$last')";
mysql_query($insert);
mysql_close();

INSERT.PHP CODE

<?
$username="removed";
$password="removed";
$database="removed";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die(" kan niet verbinden ");
$query="SELECT * FROM contacts ";
$result=mysql_query($query);

$num=mysql_numrows($result);
mysql_close();

print "Database Output";

$i=0;
while($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");

$i++;

}

print $first . " " . $last;
?>

i hope anyone can help me .. thanks a lot

    Hi there - welcome to PHPBuilder.

    1. I removed some MySQL connection info from your post... some may not view username/DB name as sensitive, but you also forgot to remove the password from the second code snippet.

    2. When posting PHP code, please use the board's

      bbcode tags - they make the code much, much easier to read (as well as aiding the diagnosis of syntax errors). You should edit your post now and add these bbcode tags to help others in reading/analyzing your code.

    3. I suspect you're trying to use the string 'localhost' in your call to [man]mysql_connect/man so you need quotes around localhost. Otherwise, you're trying to say that localhost is a defined constant.

    4. Never use user-supplied data in a query - sanitize it first! It can not only cause errors in your query, but open you up to SQL Injection vulnerabilities. Google that term and you'll learn more. Basically, you need to use a function like [man]addslashes/man (or even [man]mysql_real_escape_string/man) like so:

      if(get_magic_quotes_gpc()) {
          $first=(isset($_POST['first']) ? stripslashes($_POST['first']) : '';
          $last=(isset($_POST['last']) ? stripslashes($_POST['last']) : '';
      } else {
          $first=(isset($_POST['first']) ? $_POST['first'] : '';
          $last=(isset($_POST['last']) ? $_POST['last'] : '';
      }
      // ...
      $insert = "INSERT INTO contacts VALUES ('','" . addslashes($first) . "','" . addslashes($last) . "')";
    5. It's much easier (and cleaner) to loop through a result set using a function like [man]mysql_fetch_assoc[/man]. Instead of this:

      $num=mysql_numrows($result);
      // ...
      $i=0;
      while($i < $num) {
      $first=mysql_result($result,$i,"first");
      $last=mysql_result($result,$i,"last");
      
      $i++;
      }

      You'd simply have this:

      while($data = mysql_fetch_assoc($result)) {
          echo $data['first'] . ' ' . $data['last'];
      }

      7. Since you're only using the columns 'first' and 'last', I would highly recommend you change your "SELECT " statement to "SELECT first, last". Why? Doing SELECT uses the most resources - CPU, memory, hard drive seek time - wastefully.

      Even if you didn't change your script to the format I showed, you have a couple of problems with your code:

      1. You close your connect to MySQL ([man]mysql_close/man) before you ever retrieve the data (using [man]mysql_result/man in your code)!

      2. Your print statement that outputs the $first and $last variables is outside of the while() loop. This means that each iteration through the loop simply overwrites these variables' previous values before you ever attempt to use them. If you'll notice in my coding snippet, the echo() statement was inside the loop, allowing you to work with the values returned on each iteration of the loop.

      thanks for the very good reply...
      i followed all of your tips.. but it still gives the error with numrows..

        Migz wrote:

        but it still gives the error with numrows..

        Then you didn't follow all of my tips... because my tips included removing [man]mysql_num_rows/man entirely.

        Re-post your code so we can take a look at your changes. Also, not that 'mysql_numrows' is deprecated and has been replaced with [man]mysql_num_rows/man.

          ok .. i deleted everything that had to do with num_rows

          mainpage code

          <form action="insert.php" method="post">
          First Name: <input type="text" name="first"><br />
          Last Name : <input type="text" name="last"><br />
          <input type="submit">
          </form>
          
          <?
          $username="removed";
          $password="removed";
          $database="removed";
          
          $first=$_POST['first'];
          $last=$_POST['last'];
          
          mysql_connect("localhost",$username,$password);
          @mysql_select_db($database) or die( " kan geen verbinding maken met database ");
          $insert = "INSERT INTO contacts VALUES ('','$first','$last')";
          mysql_query($insert);
          
          ?>

          insert page code

          <?
          $username="removed";
          $password="removed";
          $database="removed";
          
          mysql_connect(localhost,$username,$password);
          @mysql_select_db($database) or die(" kan niet verbinden ");
          $query="SELECT 'first','last' FROM contacts ";
          $result=mysql_query($query);
          
          
          
          
          print "Database Output";
          
          
          $first=mysql_result($result,"first");
          $last=mysql_result($result,"last");
          
          
          print $first . " " . $last;
          
          mysql_close();
          
          
          ?>
            1. When listing columns/tables/databases in MySQL, you should surround them with ` instead of ' .

            2. [man]mysql_result/man expects the second parameter to be the row # from which you're trying to retrieve data. If you only want to see data from the first row returned, you can specify 0 (or, once again, just use a function that retrieves a row such as [man]mysql_fetch_assoc/man).

              thanks for the replys.. but im sorry to tell you that i still dont get it very much
              im very new to php ... could you maybe post a corrected version of my code that will work ..
              if this is not a problem? thank you very much

                Migz wrote:

                could you maybe post a corrected version of my code that will work

                I don't like to write code for people, but since you seem to be new, I suppose I can type out a quick example..

                This would depend, of course, upon what you're trying to do. Are you trying to display all names in the database, or just the first one? I'm going to assume you want to retrieve all of them. In that case...

                <?php
                $username="removed";
                $password="removed";
                $database="removed";
                
                mysql_connect('localhost',$username,$password);
                @mysql_select_db($database) or die(" kan niet verbinden ");
                $query="SELECT `first`,`last` FROM `contacts`";
                $result=mysql_query($query) or die('MySQL error - ' . mysql_error());
                
                print "Database Output <br>\n";
                
                while($data = mysql_fetch_assoc($result)) {
                    print $data['first'] . " " . $data['last'] . "<br>\n";
                }
                
                mysql_close();
                ?>

                  thanks a lot mate !
                  it works i think.. only error i get is

                   query error Table 'censored_be.contacts' doesn't exist 

                  but ill have a close look ant try to fix it ..

                    Write a Reply...