www.webdeveloper.com
Results 1 to 2 of 2

Thread: Format SQL output

  1. #1
    Join Date
    Sep 2009
    Posts
    8

    Format SQL output

    this is my first attempt at creating a php based sql query, so please don't laugh too hard at me..

    my table structure is thus:
    Code:
    xdata_data:
    |---------------------------------------|
    | field_id  |  user_id  | xdata_value   |
    |---------------------------------------|
    |     10    |      3      |    Lantean  |
    |     11    |      3      |    Priest   |
    |     12    |      3      |    Knight   |
    |     13    |      3      |    50       |
    |     14    |      3      |    50       |
    |     10    |      2      |    Izzymage |
    |     11    |      2      |    Mage     |
    |     12    |      2      |    Rogue    |
    |     13    |      2      |    42       |
    |     14    |      2      |    31       |
    -----------------------------------------

    My php is this:
    Code:
    <?php
    // show all errors while still developing:
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    // use require instead of include since rest of script depends on this:
    require 'config.php';
    
    mysql_connect("localhost","$mysql_u","$mysql_p") or
            die("DB connection failed - " . mysql_error());
    mysql_select_db("$mysql_db") or die ("DB select failed - " . mysql_error());
        
    $query = "SELECT data FROM xdata_data WHERE user_id = '*' ";
    $result = mysql_query($query) or die("Query failed ($query) - " . mysql_error());
    if(mysql_num_rows($result))
    {
       while($row = mysql_fetch_assoc($result))
       {
          $data = $row['xdata_value'] ;
         echo ("$data");
       }
    }
    else
    {
       echo "<p>No matches were found in the database for your query.</p>\n";
    }
    
    ?>
    output looks like this:

    Code:
    LanteanPriestKnight5050IzzymageMageScout4231
    I want to post the information into tables that resemble this:
    Code:
    ____________________________________
    | Lantean    |   Priest - Knight   |
    |            |       50 - 50       |
    |----------------------------------|
    | Izzymage   |   Mage - Rogue      |
    |            |       42 - 31       |
    |----------------------------------|

    How would i go about formating the output like that?

    Any help would be most appreciated.

    ArcAiN6

  2. #2
    Join Date
    Sep 2009
    Posts
    8
    hrm... looks like a pasted the wrong php code... sorry...

    Code:
    <?php
    // show all errors while still developing:
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    // use require instead of include since rest of script depends on this:
    require 'config.php';
    
    mysql_connect("$dbhost","$dbuname","$dbpass") or
            die("DB connection failed - " . mysql_error());
    mysql_select_db("$dbname") or die ("DB select failed - " . mysql_error());
    
    $query = "SELECT xdata_value FROM xdata_data WHERE user_id <= 200 ";
    $result = mysql_query($query) or die("Query failed ($query) - " . mysql_error());
    if(mysql_num_rows($result))
    {
       while($row = mysql_fetch_assoc($result))
       {
          $data = $row['xdata_value'] ;
         echo ("$data");
       }
    }
    else
    {
       echo "<p>No matches were found in the database for your query.</p>\n";
    }
    
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles