iPixel Posted August 27, 2007 Share Posted August 27, 2007 Basically im pulling records from a mysql database. and placing the results into a simple array.. but when i echo the array values for some reason the first row/record gets skipped and the last one turn out blank as if the values got shifted or something Code: $imgID = $_GET['imgID']; $mdl = $_GET['mdl']; $clixpix = array(); mysql_select_db($database_stool, $stool); $query = "SELECT * FROM PRODUCT_IMAGE WHERE ITEM_KEY = '$mdl' AND IMAGE_TYPE_KEY = '1' AND ACTIVE_STATUS_KEY = '1' ORDER BY PROD_IMAGE_KEY ASC"; $sql = mysql_query($query) or die('Crapped Out Here' . mysql_error()); $result = mysql_fetch_assoc($sql); $count = mysql_num_rows($sql); while($result = mysql_fetch_assoc($sql)) { //$img = $result['IMAGE_URL']; array_push($clixpix, $result['IMAGE_URL']); } for($i=0; $i<$count; $i++) { echo "<font color=\"#FF0000\">ARRAY : [ " . $i . " ]" . $clixpix[$i] . "<BR>"; } Results : ======= ARRAY : [ 0 ]240159_04 ARRAY : [ 1 ]240159_05 ARRAY : [ 2 ]240159_10 ARRAY : [ 3 ]240159_09 ARRAY : [ 4 ]240159_07 ARRAY : [ 5 ]240159_08 ARRAY : [ 6 ]240159_11 ARRAY : [ 7 ] Should be : ========= ARRAY : [ 0 ]240159_03 ARRAY : [ 1 ]240159_04 ARRAY : [ 2 ]240159_05 ARRAY : [ 3 ]240159_10 ARRAY : [ 4 ]240159_09 ARRAY : [ 5 ]240159_07 ARRAY : [ 6 ]240159_08 ARRAY : [ 7 ]240159_11 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/66925-solved-skips-first-record-why/ Share on other sites More sharing options...
sasa Posted August 27, 2007 Share Posted August 27, 2007 remove line 8 $imgID = $_GET['imgID']; $mdl = $_GET['mdl']; $clixpix = array(); mysql_select_db($database_stool, $stool); $query = "SELECT * FROM PRODUCT_IMAGE WHERE ITEM_KEY = '$mdl' AND IMAGE_TYPE_KEY = '1' AND ACTIVE_STATUS_KEY = '1' ORDER BY PROD_IMAGE_KEY ASC"; $sql = mysql_query($query) or die('Crapped Out Here' . mysql_error()); //$result = mysql_fetch_assoc($sql); <-- remove this line $count = mysql_num_rows($sql); while($result = mysql_fetch_assoc($sql)) { //$img = $result['IMAGE_URL']; array_push($clixpix, $result['IMAGE_URL']); } for($i=0; $i<$count; $i++) { echo "<font color=\"#FF0000\">ARRAY : [ " . $i . " ]" . $clixpix[$i] . "<BR>"; } Quote Link to comment https://forums.phpfreaks.com/topic/66925-solved-skips-first-record-why/#findComment-335535 Share on other sites More sharing options...
dbo Posted August 27, 2007 Share Posted August 27, 2007 Why are you using 2 loops in the first place? That could all be done with a single loop. Overall, it doesn't change the runtime much... it's still O(n) but the extra loop really makes it O(2n). That being said the change I'm suggesting shouldn't have anything to do with the output... but writing simplified code certainly does not hurt when debugging. Quote Link to comment https://forums.phpfreaks.com/topic/66925-solved-skips-first-record-why/#findComment-335536 Share on other sites More sharing options...
dbo Posted August 27, 2007 Share Posted August 27, 2007 $imgID = $_GET['imgID']; $mdl = $_GET['mdl']; $clixpix = array(); mysql_select_db($database_stool, $stool); $query = "SELECT * FROM PRODUCT_IMAGE WHERE ITEM_KEY = '$mdl' AND IMAGE_TYPE_KEY = '1' AND ACTIVE_STATUS_KEY = '1' ORDER BY PROD_IMAGE_KEY ASC"; $sql = mysql_query($query) or die('Crapped Out Here' . mysql_error()); while($result = mysql_fetch_assoc($sql)) { //$img = $result['IMAGE_URL']; array_push($clixpix, $result['IMAGE_URL']); echo "<font color=\"#FF0000\">ARRAY : [ " . $i . " ]" . $result['IMAGE_URL'] . "<BR>"; } I'm assuming that you have a reason for storing th stuff in an array as well. But the extra loop is unnecessary. Quote Link to comment https://forums.phpfreaks.com/topic/66925-solved-skips-first-record-why/#findComment-335540 Share on other sites More sharing options...
iPixel Posted August 27, 2007 Author Share Posted August 27, 2007 LoL thnx #2, line 8 removal fixed it... always something silly and retarded lol. Yea ill combine the 2 loops into 1, i was just in a state where anything was worth trying lol. thanks again Quote Link to comment https://forums.phpfreaks.com/topic/66925-solved-skips-first-record-why/#findComment-335546 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.