Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2006
    Location
    Canada
    Posts
    195
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to Select all users?

    how do i get all the users that are listed in my database to show up in a table?

    would it be something like ?

    <?php SELECT username, id FROM 'users' WHERE rank by 'id' ?>

    how would i do that? and how would i make it into a table?
    Rawli

  • #2
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to send your query to the server via mysql_query(). The doc page has an example.

  • #3
    Regular Coder
    Join Date
    May 2006
    Location
    Canada
    Posts
    195
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so i would use this code?


    PHP Code:
    <?php
    // This could be supplied by a user, for example
    $firstname 'fred';
    $lastname  'fox';

    // Formulate Query
    // This is the best way to perform a SQL query
    // For more examples, see mysql_real_escape_string()
    $query sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",
       
    mysql_real_escape_string($firstname),
       
    mysql_real_escape_string($lastname));

    // Perform Query
    $result mysql_query($query);

    // Check result
    // This shows the actual query sent to MySQL, and the error. Useful for debugging.
    if (!$result) {
       
    $message  'Invalid query: ' mysql_error() . "\n";
       
    $message .= 'Whole query: ' $query;
       die(
    $message);
    }

    // Use result
    // Attempting to print $result won't allow access to information in the resource
    // One of the mysql result functions must be used
    // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
    while ($row mysql_fetch_assoc($result)) {
       echo 
    $row['firstname'];
       echo 
    $row['lastname'];
       echo 
    $row['address'];
       echo 
    $row['age'];
    }

    // Free the resources associated with the result set
    // This is done automatically at the end of the script
    mysql_free_result($result);
    ?>

    ok so i edited that and it didnt work it just showed errors this is what i had

    PHP Code:
    <?php
    // This could be supplied by a user, for example
    $username 'Rawlijr';
    $id  '1';

    // Formulate Query
    // This is the best way to perform a SQL query
    // For more examples, see mysql_real_escape_string()
    $query sprintf("SELECT username, id FROM users WHERE username='%s' AND id='%s'",
       
    mysql_real_escape_string($username),
       
    mysql_real_escape_string($id));

    // Perform Query
    $result mysql_query($query);

    // Check result
    // This shows the actual query sent to MySQL, and the error. Useful for debugging.
    if (!$result) {
       
    $message  'Invalid query: ' mysql_error() . "\n";
       
    $message .= 'Whole query: ' $query;
       die(
    $message);
    }

    // Use result
    // Attempting to print $result won't allow access to information in the resource
    // One of the mysql result functions must be used
    // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
    while ($row mysql_fetch_assoc($result)) {
       echo 
    $row['username'];
       echo 
    $row['id'];
    }

    // Free the resources associated with the result set
    // This is done automatically at the end of the script
    mysql_free_result($result);
    ?>
    Last edited by RawliJr; 07-28-2006 at 11:25 PM.
    Rawli

  • #4
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Pretty much. The important parts are mysql_query() to send your query to the server and retrieve the result resource that contains your data, and one of the mysql_fetch_* functions to extract usable data from the result resource.

    Note you will also need to use mysql_connect() and mysql_select_db() to establish a connection and choose a database before you can send any queries.

    Read up on those functions, and you should be on your way.

  • #5
    Regular Coder
    Join Date
    May 2006
    Location
    Canada
    Posts
    195
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php  
        mysql_connect
    'Localhost''xxx''xxx' );  
        
    mysql_select_db'xxx' ); 

    $sql "SELECT id, username,  FROM users ORDER BY id DESC LIMIT 0,10";  

    if (
    $top10sql mysql_query($sql))   
    {   
         echo 
    '<table border="0" align="center" width="400" cellspacing="2" cellpadding="2">
      <tr>
        <td> Id</td>
        <td> Username </td>
      </tr>'
    ;   
         while (
    $disp_users mysql_fetch_assoc($top10sql))   
         {   
              echo 
    '  <tr>';   
              echo 
    '    <td>' $disp_users['id'] . '</td>';
              echo 
    '    <td>' $disp_users['username'] . '</td>';
              echo 
    '  </tr>';
         }   
         echo 
    '</table>';   
    }   
    ?>

    ok i tried something else, this code above, bit it doesnt show anything how come? and i have th rights fields and stuff?
    Rawli

  • #6
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The comma after username is causing a syntax error.

    For debugging this sort of thing, you can use the mysql_error() function -- it will return the db's error message upon failure.

  • #7
    Regular Coder
    Join Date
    May 2006
    Location
    Canada
    Posts
    195
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanx i got it
    Rawli

  • #8
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,631
    Thanks
    4
    Thanked 147 Times in 138 Posts
    As an aside, do your query in mysql first. then once you know the query is correct and returning the proper rows, then write the code for it in PHP or ColdFusion or whatever. If you do it in that order then you know that when the query works in mysql that any errors after that would result from the PHP code.


  •  

    Posting Permissions

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