-1

I'm using the following code to display rows from a MySQL table in a html table. I've already defined the table headers in the php - please can anyone advise how I modify the code to stop it bringing the MySQL column headings into the table as well?

Currently, it's displaying the table headings (row 1), then the MySQL column headings (row 2), before the records begin being displayed on row 3.

I've tried using -N at various points in the code to suppress the column headings, but it just throws an error.

<?php

// Create connection
$con = mysql_connect("localhost","xxxxxxxxxx","xxxxxxxxx");
// Check connection
if (!$con) {
    die("Connection failed: " . mysql_error());
} 

mysql_select_db("xxxxxxxxxxxx",$con);
$sql = "SELECT * FROM SiteList";
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>ID</th>
<th>Site_Name</th>
<th>Weather_Forecast</th>
<th>Forecast_Date</th>
<th>Forecast_Time</th>
<th>Further_Information_about_This_Site</th>
</tr>";
while($record = mysql_fetch_array($myData)) {
echo "<tr>";
echo "<td>" . $record['ID'] . "</td>";
echo "<td>" . $record['Site_Name'] . "</td>";
echo "<td>" . $record['Weather_Forecast'] . "</td>";
echo "<td>" . $record['Forecast_Date'] . "</td>";
echo "<td>" . $record['Forecast_Time'] . "</td>";
echo "<td>" . $record['Further_Information_about_This_Site'] . "</td>";
echo "</tr>";
}

echo "</table>";

mysql_close($con);

?>
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Jon295087
  • 731
  • 1
  • 8
  • 28
  • Nope, it won't do that. Those rows must be in your database. – Lightness Races in Orbit Jan 14 '17 at 21:32
  • Hi @LightnessRacesinOrbit So you can suppress column headings in a normal select query (like discussed here http://stackoverflow.com/questions/16101495/how-can-i-suppress-column-header-output-for-a-single-sql-statement ), but you can't do the same thing when calling through php? Surely there's a way? – Jon295087 Jan 14 '17 at 21:43
  • You misunderstood. What I am saying is that there is nothing to suppress. Neither the MySQL CLI or PHP will inject column headings into your resultset. The values you're seeing must actually be part of your data (presumably you mistakenly put them there). You can simply go into your database and have a look, to verify this. – Lightness Races in Orbit Jan 14 '17 at 21:49
  • _"I've tried using -N at various points in the code to suppress the column headings, but it just throws an error."_ **Programming by guessing doesn't work.** Switches to the `mysql` command-line utility have absolutely nothing to do with PHP or the MySQL PHP library. – Lightness Races in Orbit Jan 14 '17 at 21:50
  • @LightnessRacesinOrbit Apologies and forgive me - you're absolutely correct. When I've gone back and checked my db content I've realised I must have inadvertently replicated the MySQL column headings into the second row of db. Deleted them out now and getting the results I require. Thank you, and apologies for newbie oversight on my part. – Jon295087 Jan 14 '17 at 21:56

1 Answers1

1

You are looping over the data retrieved by from the DB. So the records must exist in the database. If you still want to ignore the data

$skip = 0; // declare coutner
while($record = mysql_fetch_array($myData)) {

if ($skip != 0){ // skip on first loop
echo "<tr>";
echo "<td>" . $record['ID'] . "</td>";
echo "<td>" . $record['Site_Name'] . "</td>";
echo "<td>" . $record['Weather_Forecast'] . "</td>";
echo "<td>" . $record['Forecast_Date'] . "</td>";
echo "<td>" . $record['Forecast_Time'] . "</td>";
echo "<td>" . $record['Further_Information_about_This_Site'] . "</td>";
echo "</tr>";
 }

$skip++; // increment counter

}
nrm
  • 153
  • 1
  • 6
  • Thank you for taking a look @nrm. See comment above, you're correct, the data exists in the db, i'd mistakenly replicated the column headers as row 2 in the db. – Jon295087 Jan 14 '17 at 21:59