Hi all,
I'm new to php. From an asp/vbs/mssql background I found it incredibly easy to learn and work with, but I have noticed some peculiarities, one of which is this.
This code should query a database for all the entries in table accounts, print a table row for each entry in the sql table, and a table cell for each column in the sql table:
$query = "Select * from accounts;";
if (!($result = mysql_query($query, $conn)))
die("Could not process request"
;
while ($row = @ mysql_fetch_array($result))
{
print("<tr>"
;
foreach($row as $fieldname)
{
print("<td>" . $fieldname . "</td>"
;
}
print("</tr>"
;
}
However what I found is that for some reason, each column was being printed twice (the foreach loop was executing twice for each $fieldname). I didn't let it baffle me for long, it was easily fixed by using MYSQL_BOTH in mysql_fetch_array and by adding a counter to the loop and using $row[$cnt] instead of $fieldname. However, anal as I am, it will forever bother me until I figure out why that happened. I don't see anything in the code that should have caused it.
Thanks for any feedback
I'm new to php. From an asp/vbs/mssql background I found it incredibly easy to learn and work with, but I have noticed some peculiarities, one of which is this.
This code should query a database for all the entries in table accounts, print a table row for each entry in the sql table, and a table cell for each column in the sql table:
$query = "Select * from accounts;";
if (!($result = mysql_query($query, $conn)))
die("Could not process request"
while ($row = @ mysql_fetch_array($result))
{
print("<tr>"
foreach($row as $fieldname)
{
print("<td>" . $fieldname . "</td>"
}
print("</tr>"
}
However what I found is that for some reason, each column was being printed twice (the foreach loop was executing twice for each $fieldname). I didn't let it baffle me for long, it was easily fixed by using MYSQL_BOTH in mysql_fetch_array and by adding a counter to the loop and using $row[$cnt] instead of $fieldname. However, anal as I am, it will forever bother me until I figure out why that happened. I don't see anything in the code that should have caused it.
Thanks for any feedback