Lose the last contact after a mysql_fetch_array in loop
Posted: Mon Aug 18, 2003 12:50 pm
Ok here is the problem. I am pulling data from a MySQL database and it is loseing the last item in a for loop. Basically if it finds 5 contacts to display it will count them properly using num_results, but when I try to display it, it only show's 4 of them !
It is long and messy right now, but it should display properly. Like I said I get the correct count in the num_results, but it always drops the last data row in my loop when I try to display it!! There is alot of html mixed in there with it, so it might get confusing. If I need to repost this in a cleaner fashion, please let me know. I have saw this problem on other forums, but no one had a solution.
Code: Select all
<?php //Looking for contacts and bulding table for contacts!
$contact_result = mysql_query("SELECT * FROM contact WHERE company_id = '".($companyї"company_id"])."' ORDER BY contact.last_name ASC") or die("<b>MySQL Error:</b> ".mysql_errno()." : ".mysql_error());
$contact = mysql_fetch_array($contact_result);
$num_results = mysql_num_rows($contact_result);
if (!($contactї"company_id"])=="") //If company_id exists, then build list
{
//Create phone number for display
if ((!$contactї"direct_phone"])=="")
{
$db_direct_phone = ($contactї"direct_phone"]);
$new_direct_phone = "(".substr($db_direct_phone, -10, 3).") ".substr($db_direct_phone, -7, 3)."-".substr($db_direct_phone, -4, 4);
}
?>
<tr bgcolor="#D3D3D3"><td colspan=2><font face="Arial, Helvetica" size="2" color="Black"><b>Company Contacts</b></font></td></tr>
<tr>
<td colspan=2 width=100%>
<table border=0 width=100%>
<tr>
<td width=175><font face='Arial,Helvetica' size='2'
color='Black'><b>Name</b></td>
<td><font face='Arial,Helvetica' size='2' color='Black'><b>Direct Phone/Extension</b></td>
<td><font face='Arial,Helvetica' size='2' color='Black'><b>Email</b></td>
</tr>
<?php
echo $num_results . " contact(s) in the database for his company.";
for ($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($contact_result);
?>
<tr><td><font face='Arial,Helvetica' size='2' color='Black'>
<a href='view_results.php?id_contact=<?php echo ($rowї'contact_id']); ?>'>
<?php echo ($rowї"first_name"])." ".($rowї"last_name"]); ?> </a>
</font></td>
<td><font face='Arial,Helvetica' size='2' color='Black'>
<?php
if((!$rowї"direct_phone"])=="")
{
$db_direct_phone = ($rowї"direct_phone"]);
$new_direct_phone = "(".substr($db_direct_phone, -10, 3).") ".substr($db_direct_phone, -7, 3)."-".substr($db_direct_phone, -4, 4);
echo $new_direct_phone;
}
else if((!$rowї"extension"])==""){ echo " Ext ". ($rowї"extension"])." "; }
?>
</font></td>
<td><font face='Arial,Helvetica' size='2' color='Black'>
<?php
if (!$rowї"email"]==""){echo "<a href='mailto: " . ($rowї"email"]) . "'>Email</a>";
}
?>
</font></td>
</tr>
<?php
}
}
?>
</table>
</td>
</tr>