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 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Simple DB Query and Display values

    Hi all, I have an application where I have to display the score of each battle on an html page based on the DB table below. How would I do this?

    For example, I would like to display something like this all on the same page (based on the 3 battles in example db table below):

    Participants: 987 vs. 988
    Score: 23 to 65

    Participants: 658 vs. 784
    Score: 45 to 75

    Participants: 365 vs. 355
    Score: 78 to 52


    Code:
    battle_id   user_id1   user_id2   score1   score2
    
    10           987       988        23       65
    11           658       784        45      75
    12           365       355        78      52
    What is the code I need to have on the php page and also in the html portion to display the results? Please excuse my novice, I'm not a programmer and my developer left me mid way.. Just need advice on some simple stuff and hopefully learn a thing or two along the way.

    Thanks so much for the help, much appreciated.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I assume you showed us the names of your MySQL variables,
    but what is the name of the table itself?

  • #3
    New Coder
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Correct, the table name is just "battles".

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    PHP Code:
    <?php

    // Open Your MySQL Database with Your login.
    $dbhost 'hostname';
    $dbuser 'username';
    $dbpass 'password';
    $dbname 'databasename';

    // Connect to the database.
    $conn mysql_connect($dbhost$dbuser$dbpass) or die('Error connecting to mysql');
    mysql_select_db($dbname) or die('Cannot select database');

    // Query the database.
    $query="SELECT * FROM battles ORDER BY battle_id ASC";
    $result mysql_query($query) or die(mysql_error());

    // Close the database.
    mysql_close($conn);

    // Print your table title line.
    echo"
    <table cellspacing='5'>
    <tr>
    <td>battle_id</td>
    <td>user_id1</td>
    <td>user_id2</td>
    <td>score1</td>
    <td>score2</td>
    </tr>
    "
    ;

    // Fetch and Display Table Data.
    while ($row mysql_fetch_assoc($result)) {
       echo
    "
       <tr>
       <td>{$row['battle_id']}</td>
       <td>{$row['user_id1']}</td>
       <td>{$row['user_id2']}</td>
       <td>{$row['score1']}</td>
       <td>{$row['score2']}</td>
       </tr>
       "
    ;
    }

    echo 
    "</table>";
    ?>


  •  

    Posting Permissions

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