...

View Full Version : Simple DB Query and Display values



SneakerHead
03-19-2010, 07:13 PM
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




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.

mlseim
03-19-2010, 07:36 PM
I assume you showed us the names of your MySQL variables,
but what is the name of the table itself?

SneakerHead
03-19-2010, 08:25 PM
Correct, the table name is just "battles".

mlseim
03-20-2010, 12:30 AM
<?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>";
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum