rabadaba Posted April 1, 2008 Share Posted April 1, 2008 I have just stared to get my first PHP- MySQL code working. I have a couple of odd behaviours occurring. Note that I have replicated this problem on two systems - one my own PC and also on my host provider system and the results are identical - thus my issues are related to my code or DB structure. 1. On one table - I lost the first row in my results - so I created a row of id=0 and other null fields and then the results looked correct. The 'zeroth' id did not display in my results which looks correct - but the coding is dropping the first entry and I want to know why. Code is below (note that I place the results into an array since I need to run a query twice: mysql_select_db($database_dsStockPhotoTree, $dsStockPhotoTree); $query_rsSidebar = "SELECT * FROM sidebar ORDER BY id ASC"; $rsSidebar = mysql_query($query_rsSidebar, $dsStockPhotoTree) or die(mysql_error()); $row_rsSidebar = mysql_fetch_assoc($rsSidebar); $totalRows_rsSidebar = mysql_num_rows($rsSidebar); $rsSidebarArray = array(); while($row = mysql_fetch_array($rsSidebar, MYSQL_ASSOC)) { $rsSidebarArray[] = $row; } and in another section ..... $sidebar = ''; foreach($rsSidebarArray as $rowNum => $row) { if ($row['subtitle'] == '1') // means it is a sidebar title i.e. not an <a> tag {$sidebar .= '<li class="subtitle">'.$row['name'].'</li>'."\n"; } elseif ($row['id'] == $sel_id) // means it is the current page and we want to set class = current {$sidebar .= '<li><a class="current" href="http://www.stockphototree.com/category.php?id=' . $row['id'] .'">' .$row['name'] .' == >></a></li>'."\n"; } else {$sidebar .= '<li><a href="http://www.stockphototree.com/category.php?id=' . $row['id'] . '">' . $row['name'] . '</a></li>' . "\n"; } // means a normal menu item } echo $sidebar; =================================== 2. In my second query - I get an extra result. The first row comes in twice in the results??? Can't have that happen! mysql_select_db($database_dsStockPhotoTree, $dsStockPhotoTree); $query_rsImages = "SELECT * FROM images ORDER BY id ASC"; $rsImages = mysql_query($query_rsImages, $dsStockPhotoTree) or die(mysql_error()); $row_rsImages = mysql_fetch_assoc($rsImages); $totalRows_rsImages = mysql_num_rows($rsImages); $sel_id = $_GET['id']; and in another section ...... $row = ''; $image_out = ''; while($row = mysql_fetch_array($rsImages, MYSQL_ASSOC)){ if ($row['cat_id'] == $sel_id) // the cat_id for the images row matches the the current page category 'id' {$image_out .= '<div id="border"><a href="xxx.php"><img src="images/' . strtolower($row['imageCode']) . '/' . $row['thumbFile'] . '.jpg" width="'; $image_out .= $row['thumbWidth'] . '" height="' . $row['thumbHeight'] .'" border="0" alt="Image: ' . $row['imageCode'] . '-'. $row['imageNum']; $image_out .= '" onMouseOver="showtrail(\'images/'; $image_out .= strtolower($row['imageCode']) . '/' . $row['dispFile'] . '.jpg\', \'' . $row['imageTitle'] . '\', ' .$row['dispHeight'] .');" onmouseout="hidetrail();" ><p class="pcaption">Image: '; $image_out .= $row['imageCode'] . '-' . $row['imageNum'] . '</p><p>' . $row['imageTitle'] .'</p></a></div>' . "\n"; } echo $image_out; } Any help on the missing and duplicated results would be appreciated. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 1, 2008 Share Posted April 1, 2008 Your missing row is because your code is fetching a row from the result set and not doing anything with it - $row_rsSidebar = mysql_fetch_assoc($rsSidebar); If you don't want to fetch and discard a row, remove the above line of code every place you are doing that (you program only does what your code tells it to do.) If you are getting two identical rows, you likely have two of them in your database. Examine the data in the database directly to see what you actually have. Quote Link to comment Share on other sites More sharing options...
rabadaba Posted April 1, 2008 Author Share Posted April 1, 2008 Thanks for your help. You got it right!!! Also note that I fixed the additional row problem. It was due to the location of the $image_out =''; being set outside the loop. Great help. Appreciate it! Quote Link to comment 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.