FourEyes Posted June 12, 2006 Share Posted June 12, 2006 I am reading data out of a MySQL table. I know there are 144 fields per record, and I've checked the data to ensure this is the case. However, when I read the values into an array, I end up with 288 elements in my array. Array element 0 is duplicated in element 1, 2 is duplicated in 3, etc. What's causing this and how do I stop it?<?phpsession_start();header("Cache-control: private"); $person_id = $_SESSION['page'];$customer_id = $_SESSION['stamp'];$c = $_SESSION['c'];$m1 = $_SESSION['m1'];$m2 = $_SESSION['m2'];$medical = array(array());include 'header.html';# Connect to MySQL$connection = @mysql_connect("localhost", "???", "???") or die("Could not connect to MySQL.");# Select the specified database.$result = @mysql_select_db("sfb", $connection) or die("Could not select the requested database.");?><table width=175 border=0 align="left" cellpadding=0 cellspacing=0><tr align="left" valign="top" nowrap> <td width=175 valign="top" nowrap><img src="../images/NAV-pic.jpg" border=0></td></tr><tr align="left" valign="top" nowrap> <td align="left" nowrap><a href="index.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image6','','NAV-home-on.gif',0)"><img src="../images/NAV-home.gif" alt="home" name="Image6" border=0></a></td></tr><tr align="left" valign="top" nowrap> <td align="left" nowrap><a href="products.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image7','','NAV-products-on.gif',0)"><img src="../images/NAV-products.gif" alt="Products & Services" name="Image7" border=0></a></td></tr><tr align="left" valign="top" nowrap> <td align="left" nowrap><a href="csa.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image19','','NAV-service-on.gif',0)"><img src="../images/NAV-service-on.gif" alt="Customer Service Area" name="Image19" border=0></a></td></tr><tr align="left" valign="top" nowrap> <td align="left" nowrap><a href="quotes.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image9','','NAV-quotes-on.gif',0)"><img src="../images/NAV-quotes.gif" alt="Online Quotes" name="Image9" border=0></a></td></tr><tr align="left" valign="top" nowrap> <td align="left" nowrap><a href="free.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image17','','NAV-info-on.gif',0)"><img src="../images/NAV-info.gif" alt="Free Information" name="Image17" border=0></a></td></tr><tr align="left" valign="top" nowrap> <td align="left" nowrap><a href="about.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image18','','NAV-about-on.gif',0)"><img src="../images/NAV-about.gif" alt="About Us" name="Image18" width=175 height=24 border=0></a></td></tr> </table><?php$self = $_SERVER['PHP_SELF'];echo ("<form action=\"$self\" method=\"post\">");?><h2><?php echo $c;?><br>Add/Edit/Delete Major Medical Plan</h2> <select name="Plans" size=5><?php$query = "select * from medical order by plan_name";$result = mysql_query($query,$connection) or die ("Could not find any plans.");$i = 0;while ($row = mysql_fetch_array($result)) { $id = $row['plan_id']; if ($id == $_POST['Plans']) { echo (" <option selected value="); } else { echo (" <option value="); } echo $id . ">" . $row['plan_name'] . " ~ " . $row['carrier']; if ($row['network'] == "Y") { echo " ~ Network</option>\n"; } else { echo " ~ Non-network</option>\n"; } $j = 0; foreach ($row as $info) { $medical[$i][$j] = $info; $j++; } $i++;}?></select><br>Select a plan, then click, <input type="submit" name="View" value="View Plan Details"><br><br><table width=595 border=0 cellpadding=0 cellspacing=0><?phpecho $medical[0][2],$medical[1][2];echo $medical[0][3],$medical[1][3];# The two lines above produce the same results = Badness.for ($k=4;$k<$j;$k=$k+7) { echo "<tr><td>" . $medical[0][$k] . "</td></tr>";}?></table><!--#include virtual="footer.html" --> Quote Link to comment https://forums.phpfreaks.com/topic/11805-how-do-i-stop-duplicate-array-entries/ Share on other sites More sharing options...
toplay Posted June 12, 2006 Share Posted June 12, 2006 change this:while ($row = mysql_fetch_array($result)) {to this:while ($row = mysql_fetch_assoc($result)) {Read up in the manual why! Quote Link to comment https://forums.phpfreaks.com/topic/11805-how-do-i-stop-duplicate-array-entries/#findComment-44760 Share on other sites More sharing options...
FourEyes Posted June 13, 2006 Author Share Posted June 13, 2006 [!--quoteo(post=383002:date=Jun 12 2006, 06:41 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ Jun 12 2006, 06:41 PM) [snapback]383002[/snapback][/div][div class=\'quotemain\'][!--quotec--]change this:while ($row = mysql_fetch_array($result)) {to this:while ($row = mysql_fetch_assoc($result)) {Read up in the manual why![/quote]<sound of hand slapping forehead>From the PHP manual page on mysql_fetch_array:ParametersresultThe result resource that is being evaluated. This result comes from a call to mysql_query().result_typeThe type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and the default value of MYSQL_BOTH. Road hypnosis. I use mysql_fetch_array all the time, and just got so used to it I didn't *think* about using another fetch option. Thank you, ToPlay Quote Link to comment https://forums.phpfreaks.com/topic/11805-how-do-i-stop-duplicate-array-entries/#findComment-45196 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.