Page 1 of 1

data display

Posted: Mon Apr 04, 2011 3:13 pm
by ste_512
Hi everyone, I am using the following code to display my database data and it works very well. Is there a way that i can edit the code so i can just veiw different elements of the datase. for example, may database has 4 feilds hometeam, awayteam, score1,score2. is there a way i can just call up the results from the hometeam if for example that team was called rovers?

Thanks for all your help

Ste

<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = 'localhost';
$db_user = '*****';
$db_pwd = '*****';

$database = '*******';
$table = '********';

if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");

if (!mysql_select_db($database))
die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";

// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";

echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>

Re: data display

Posted: Mon Apr 04, 2011 5:24 pm
by TonsOfFun
Change you query:

Code: Select all

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
to:

Code: Select all

// sending query
$hometeam = 'rovers';
$result = mysql_query("SELECT * FROM {$table} WHERE hometeam = $hometeam");
if (!$result) {
die("Query to show fields from table failed");
}