Jump to content

Need A Little Help With Some Php Mysql Functions


samporras

Recommended Posts

Hi guys.

 

I'm still relatively new to php programming and I'm having some problems with some functionality that i am currently writing. I want to capture all the data from my database table and put it in a 2 dimensional array. I found this function online that seems to do the trick:

 

>function mysql_fetch_data($result, $numass=MYSQL_BOTH) {
$i=0;
$keys=array_keys(mysql_fetch_array($result, $numass));
mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result, $numass)) {
	foreach ($keys as $speckey) {
		$got[$i][$speckey]=$row[$speckey];
	}
	$i++;
}
return $got;
}

 

the problem i am having is that i want to reference each element within the master array dynamically:

 

>	$result = mysql_query($select, $conn) or die(mysql_error());
$rowcount = mysql_num_rows($result);
$properties = mysql_fetch_data($result);

for($i=1; $i<10; $i++)
{			
	if($rowcount <= $i)
	{
		echo('filename: '.$properties[$i]['filename'].'<br />');
	}
	else
	{
				   //do something else
	}
}

 

why is it that i can not reference that specific index position dynamically? when i replace my variable ($i) with a specific value, the correct value for my field 'filename' is appearing. am i missing something or doing something incorrectly?? any help would be greatly appreciated! ;)

 

 

Thanks in advance,

 

Sam

Link to comment
Share on other sites

The first thing I notice is the for loop. You are first starting from element 1 in the array ($i = 1) but PHP arrays start from 0. So change that to $i = 0;

 

Secondly you are looping through the data 9 times but you should be looping through while $i is LESS than $rowcount. So change that to $i <$rowcount;

 

Then inside the loop you are checking to see if $rowcount is LESS that or equal to $i. I don't get what you are trying to do, surely you would want it the other way around otherwise the data you are seeking in the array will not exist.

 

Personally I would change your for-loop to:

 

>for($i=0; $i<$rowcount; $i++)
{			
	  echo 'filename: '.$properties[$i]['filename'].'<br />';
}

 

No need for the check against row count because the for loop does that

No need for the else statement because it won't be in the loop under those circumstances.

 

 

I can't see a reason why that would not work off the top of my head.

 

 

Hope this helps.

Link to comment
Share on other sites

Personally I would change your for-loop to:

 

>for($i=0; $i<$rowcount; $i++)
{			
	  echo 'filename: '.$properties[$i]['filename'].'<br />';
}

 

Thanks for the help OBJ. I didn't know that in PHP the start is 0 so i've updated my for statement.

 

I didn't post the entire piece of code. The reason I was looping through 10 times is because i wanted to do something from 0 to rowcownt, and then do something else from rowcount to 10. The problem i'm having is when executing the following statement:

 

>echo 'filename: '.$properties[$i]['filename'].'<br />';

 

The result i get is just

filename:

 

when i remove $i from the index reference, the correct value shows up for the filename variable. (ie. $properties[1]['filename'])

Link to comment
Share on other sites

Hello, OK I think I understand what you were trying to do now.

 

 

At the top of your PHP file just under the <?php can you paste this line of code:

 

>error_reporting(E_ALL);

 

 

This will print any errors that occur during the script in the browser as you test the script.

 

I still think there is an error in the logic... I would change your for loop to this:

 

> for($i=0; $i<10; $i++)
{			
	if($rowcount > $i)
	{
		echo('filename: '.$properties[$i]['filename'].'<br />');
	}
	else
	{
				   //do something else
	}
}

 

The only thing I have changed is: $rowcount > $i.

 

What you were doing before is cycling through your results, then checking if $rowcount was LESS than or equal to the current value of $i. This wouldn't work and is probably the reason you are getting the output of just "filename:"

 

I shall explain why now:

 

Let's say for an example that your MySQL query returns 5 rows.

 

$rowcount therefore = 5.

 

then you start at $i = 0

 

$i =0 , $rowcount <= $i, (5 <= 0) ... FALSE...

$i =1 , $rowcount <= $i, (5 <= 1) ... FALSE...

$i =2 , $rowcount <= $i, (5 <= 2) ... FALSE...

 

....

$i = 5 , $rowcount <= $i, (5 <= 5) ... TRUE... So now we have one that is true. But as there are only 5 rows returned there will be NO value for $properties[5]['filename'], as PHP arrays begin from 0 the $properties[5] is actually the 6th element, hence no result, hence "filename: " So from then on $i =6 to $i=9 is the same result.

 

 

This I think should solve your issue.

Link to comment
Share on other sites

Join the conversation

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

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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