Results 1 to 7 of 7

Thread: Show table data

  1. #1

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362

    Show table data

    Hello
    I am new to php and I wanted to know how to show all the value of my table. I am using mysql.

    Here is my code:


    PHP Code:

    <? 
    // db variables 
    // inserts variables accordingly 
    $host = "misterp2d.dyndns.org"; 
    $user = "bye"; 
    $pass = "allo"; 
    $db = "Jeux"; 
    $table = "Usager"; 

    // connecting to the db 
    $link = mysql_connect ($host, $user, $pass) 
          or die("Impossible de se connecter : " . mysql_error()); 
           
    // the db query 
    $q = "SELECT * from $table"; 

    $results = mysql_select_db ($db); 

    // fetching the results 
        $result = mysql_query("SELECT id_Usager, name_Usager FROM Usager"); 

        while ($row = mysql_fetch_array($result)) 
       { 
            printf ("ID: %s  Name: %s", $row[0], $row[1]);  
        } 

        mysql_free_result($result); 

    // closing the db link 
    mysql_close ($link); 
    ?>

    Here is the errors I got:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp\www\test.php on line 29

    Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp\www\test.php on line 34

  2. #2

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    it work now!!!

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Since I don't agree with some of the stuff in that code, I thought I'd give my thoughts:

    PHP Code:
    <?php //#1
        // db variables 
        // inserts variables accordingly 
        
    $host "misterp2d.dyndns.org"
        
    $user "bye"
        
    $pass "allo"
        
    $db "Jeux"
        
    $table "Usager"

        
    // connecting to the db 
        
    $link mysql_connect ($host$user$pass
            or die(
    "Impossible de se connecter : " mysql_error()); 
        
    mysql_select_db($db$link); //#2

        // the db query 
        //$q = "SELECT * from $table"; //#3

        // fetching the results 
        
    $result mysql_query("SELECT id_Usager, name_Usager FROM Usager"); 

        while (
    $row mysql_fetch_array($result)) 
        { 
            
    printf ("ID: %s  Name: %s"$row[0], $row[1]);  
        } 

        
    mysql_free_result($result); //#4

        // closing the db link 
        
    mysql_close ($link); //#5
    ?>
    [list=1][*]It is recommended that you not use short tags (<?). See here.[*]Since mysql_select_db returns a BOOL, all you're doing is storing true and false, and since you're not checking whether it returned true or false, there's no need to assign it to a value.

    If you are connection to more than one database, you might want to get in the habit of specifying a resource identifier in this function as well.[*]This is not used in the code. Why have it?[*]This is generally not required in projects unless you are worried about memory. All results are freed when the script finishes execution.[*]This is not required. The resource is closed when the script finishes execution.[/list=1]
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Thx Hobo,
    PHP is so new to me, I do not understand all, thx man I will check all my code to clear it

  5. #5
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    actually, to add on to hobo's insight,

    mysql_select_db($db, $link); //#2

    that is generally used if you connect to more than one database at a time. other than that, all you need is

    mysql_select_db($db);

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by phpman
    actually, to add on to hobo's insight,

    mysql_select_db($db, $link); //#2

    that is generally used if you connect to more than one database at a time. other than that, all you need is

    mysql_select_db($db);
    I already said that:

    If you are connection to more than one database, you might want to get in the habit of specifying a resource identifier in this function as well.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    sorry, I didn't even see that line.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width