Christopher
Christopher

Reputation: 267

SQL results truncating first result

Odd series of events.

I have a webpage strictly used to build a database (super barebones). The HTML form action is to the same page - with a PHP statement if(isset($_POST['submit'])) to change content on each submit. Each time the user submits, it will echo a This [x] was successfully added., then go on to display a table at the bottom of the page.

The table is showing the results, but the first result is truncated.

Code for table:

$host = "x";
$dbusername = "x";
$dbpassword = "x";
$db_name = "x";

mysql_connect("$host","$dbusername","$dbpassword") or die("Cannot connect to database");
mysql_select_db("$db_name") or die("Cannot select database");

// Collect Information
$query = "SELECT * FROM database";

$data = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($data);

print "<table border='1px' cellpadding='4px' cellspacing='0'><tr><th>ID</th><th>CELL 1</th><th>CELL 2</th><th>CELL 3</th><th>CELL 4</th><th>CELL 5</th><th>CELL 6</th><th>CELL 7</th><th>CELL 8</th><th>CELL 9</th><th>CELL 10</th><th>CELL 11</th><th>CELL 12</th><th>CELL 13</th><th>CELL 14</th></tr>";
while($row = mysql_fetch_array($data)){
    print "<tr><td>";
print $row['index'];
print "</td><td>";
print $row['CELL 1'];
print "</td><td>";
print $row['CELL 2'];
print "</td><td>";
print $row['CELL 3'];
print "</td><td>";
print $row['CELL 4'];
print "</td><td>";
print $row['CELL 5'];
print "</td><td>";
print $row['CELL 6'];
print "</td><td>";
print $row['CELL 7'];
print "</td><td>";
print $row['CELL 8'];
print "</td><td>";
print $row['CELL 9'];
print "</td><td>";
print $row['CELL 10'];
print "</td><td>";
print $row['CELL 11'];
print "</td><td>";
print $row['CELL 12'];
print "</td><td>";
print $row['CELL 13'];
print "</td><td>";
print $row['CELL 14'];
print "</td></tr>";
}

echo "</table>";

mysql_close();

Any ideas?

Upvotes: 0

Views: 77

Answers (1)

clem
clem

Reputation: 3366

you're calling mysql_fetch_array() before you go into the loop
this loads the first row of the result into the $row variable
on calling it again in the while loop you overwrite the first result row with the second one

$data = mysql_query($query) or die(mysql_error());

**$row = mysql_fetch_array($data);**

print "<table border='1px' cellpadding='4px' cellspacing='0'><tr><th>ID</th><th>CELL 1</th><th>CELL 2</th><th>CELL 3</th><th>CELL 4</th><th>CELL 5</th><th>CELL 6</th><th>CELL 7</th><th>CELL 8</th><th>CELL 9</th><th>CELL 10</th><th>CELL 11</th><th>CELL 12</th><th>CELL 13</th><th>CELL 14</th></tr>";
while($row = mysql_fetch_array($data)){

remove this line and it will show the first row as well

Upvotes: 4

Related Questions