PHP + mySQL problems

Discussion in 'Web Design and Development' started by Poff, Feb 10, 2007.

  1. Poff macrumors 65816

    Poff

    Joined:
    Sep 16, 2003
    Location:
    Stavanger, Norway
    #1
    Hi,

    I'm trying to go through a tutorial on PHP + mySQL here.

    It's working fine until I get to the line
    PHP:
    $num=mysql_numrows($result);
    where I get the error message

    Soo... is there a fault in the tutorial? Or did I do something else wrong? Here's a copy of all of my code:

    PHP:
    <?php
    $username
    ="myusername";
    $password="mypassword";
    $database="flaatartist";

    mysql_connect("mysqldatabase",$username,$password);

    @
    mysql_select_db($database) or die("error connecting to database");

    mysql_query("INSERT INTO contacts VALUES ('Erlend','Handeland')");

    $query="SELECT * FROM contacts";
    $resulr=mysql_query($query);




    $num=mysql_numrows($result);

    mysql_close();

    echo 
    "<b>Database</b><br><br>";


    $i=0;



    while (
    $i $num) {

    echo 
    "Navn: <b>$fornavn $etternavn</b><br>";


    $i++;
    $fornavn=mysql_result($result,$i,"fornavn");
    $etternavn=mysql_result($result,$i,"etternavn");
    }
    ?>
     
  2. mac-convert macrumors 6502a

    mac-convert

    Joined:
    Nov 2, 2006
    Location:
    Are we there yet?
    #2
    $query="SELECT * FROM contacts";
    $resulr=mysql_query($query);

    Looks like a typo - resulr s/b result ???
     
  3. werther macrumors regular

    Joined:
    May 15, 2006
    #3
    $resulr should be $result

    edit: Too slow sorry...
     
  4. Poff thread starter macrumors 65816

    Poff

    Joined:
    Sep 16, 2003
    Location:
    Stavanger, Norway
    #4
    Thanks a lot! Silly mistake! :) Now it all works like a charm. (after fixing another silly mistake I found towards the bottom.. :))
     
  5. nightelf macrumors 6502

    Joined:
    Mar 25, 2003
    #5
    You could also simplify this part:

    while ($i < $num) {

    echo "Navn: <b>$fornavn $etternavn</b><br>";


    $i++;
    $fornavn=mysql_result($result,$i,"fornavn");
    $etternavn=mysql_result($result,$i,"etternavn");
    }

    to

    for ($i = 1; $i <= $num; $i++)
    {

    echo "Navn: <b>$fornavn $etternavn</b><br>";

    $fornavn=mysql_result($result,$i,"fornavn");
    $etternavn=mysql_result($result,$i,"etternavn");
    }
     

Share This Page