Jump to content

[SOLVED] Skips first record !! Why !???


Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/66925-solved-skips-first-record-why/
Share on other sites

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>";
}

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.

$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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.