I have nulls in my table but I want to print out all the rows that have idt (primary key and auto_increment) not null.
<body>
<?php
$conn = mysql_connect(connection string works);
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
else
{
mysql_select_db('db505714230');
$sql = "SELECT * FROM test;";
$result = mysql_query($sql, $conn);
echo '<center><table><tr><th>idt</th><th>datetime</th><th>col1</th><th>col2</th><th>col3</th></tr>';
$n=0;
while (($data = mysql_fetch_array($result, MYSQL_ASSOC)) !== null) {
$n=$n+1;
echo '<tr><td>', '<input name="Text' , $n , '" type="text" value= "' , $data['idt'] , '" width="25" />', '</td>';
$n=$n+1;
echo '<td>', '<input name="Text' , $n , '" type="text" value= "' , $data['datetime'] , '" width="25" />', '</td>';
$n=$n+1;
echo '<td>', '<input name="Text' , $n , '" type="text" value= "' , $data['col1'] , '" width="25" />', '</td>';
$n=$n+1;
echo '<td>', '<input name="Text' , $n , '" type="text" value= "' , $data['col2'] , '" width="25" />', '</td>';
$n=$n+1;
echo '<td>', '<input name="Text' , $n , '" type="text" value= "' , $data['col3'] , '" width="25" />', '</td></tr>';
}
mysqli_free_result($resulti);
echo '</table></center>';
}
mysql_close($conn);
?>
</body>
Answered my own question:
<body>
<?php
$conn = mysql_connect('db505714230.db.1and1.com', 'dbo505714230','electrical');
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
else
{
mysql_select_db('db505714230');
$sql = "SELECT * FROM test;";
$result = mysql_query($sql, $conn);
echo '<center><table><tr><th>idt</th><th>datetime</th><th>col1</th><th>col2</th><th>col3</th></tr>';
$n=0;
while ($data = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($data['idt'] !== null) {
$n=$n+1;
echo '<tr><td>', '<input name="Text' , $n , '" type="text" value= "' , $data['idt'] , '" width="25" />', '</td>';
$n=$n+1;
echo '<td>', '<input name="Text' , $n , '" type="text" value= "' , $data['datetime'] , '" width="25" />', '</td>';
$n=$n+1;
echo '<td>', '<input name="Text' , $n , '" type="text" value= "' , $data['col1'] , '" width="25" />', '</td>';
$n=$n+1;
echo '<td>', '<input name="Text' , $n , '" type="text" value= "' , $data['col2'] , '" width="25" />', '</td>';
$n=$n+1;
echo '<td>', '<input name="Text' , $n , '" type="text" value= "' , $data['col3'] , '" width="25" />', '</td></tr>';
}
}
echo '</table></center></div>';
}
mysql_close($conn);
?>
</body>
Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.
Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.