2 Replies - 3886 Views - Last Post: 18 September 2012 - 02:39 AM Rate Topic: -----

#1 tokei  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 15-December 11

loop array outside from the functions

Posted 17 September 2012 - 09:10 PM

Hi all,

I have a database that contains 11 data in data_employees table.
First , I create the functions and loop the data , store to the array $id,$name

At outside the function, i want use data that store in array.
What happen is $id array only store 1 data instead of 11 data.

Here is my code

<?php

require 'connection.php';

function QueryDatabase($option){
	global $id, $name;
	if($option == "selectAll"){
		$sql = "select * from data_employees";
		$query = mysql_query($sql);
		$rowCount = mysql_num_rows($query);
		echo $rowCount;   //$rowCount: 11
		
		$i=0;
		while ($fieldName = mysql_fetch_assoc($query)){
			/* store data in array */
			$id = array($i => $fieldName['id']);
			$name = array($i => $fieldName['name']);
			$i++;
			echo "<br>".count($id);
		}
	}
}

QueryDatabase('selectAll');

/* I want to loop array outside from the functions */

//display array count, for testing 
print "<br>count array out from the function :" . count($id);    //count($id) = 1 , but the actual number of data is 11

// display all the value in array here
foreach ($id as $i => $value) {
	echo "<br>value of array:";
    print_r ($value);
}

?>



I am new user in PHP, just tell me what to do.

Thanks for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: loop array outside from the functions

#2 KingCuddles  Icon User is offline

  • D.I.C Regular

Reputation: 176
  • View blog
  • Posts: 496
  • Joined: 20-December 08

Re: loop array outside from the functions

Posted 18 September 2012 - 01:05 AM

Ok,

Couple of points here:

Firstly, why would you echo things out in the function. If it was me I would return an array of results from the function, and then loop through them.

Secondly the mysql_* functions are outdated and insecure (if incorrectly used) it is much much better to switch to mysqli_* or better yet, PDO. Read this if you want to know why using mysql_* is a bad idea.

Finally, if you look at your while loop, each time it iterates it will replace $id and $name with the values, this means that they will always be equal to the final set of results your query returns.

		while ($fieldName = mysql_fetch_assoc($query)){
			/* store data in array */
			$id = array($i => $fieldName['id']);
			$name = array($i => $fieldName['name']);
			$i++;
			echo "<br>".count($id);
		}



It may prove beneficial for you to see what results your query is actually returning, try running:

$result = mysql_query('select * from data_employees');
print_r(mysql_fetch_assoc($result));



As you can see, this function returns an associative array for you, meaning you could just run a foreach loop over the results.

I would strongly suggest switching to PDO before proceeding any further though.

This post has been edited by KingCuddles: 18 September 2012 - 01:07 AM

Was This Post Helpful? 4
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,589
  • Joined: 08-August 08

Re: loop array outside from the functions

Posted 18 September 2012 - 02:39 AM

View PostKingCuddles, on 18 September 2012 - 04:05 AM, said:

I would strongly suggest switching to PDO before proceeding any further though.

Quoted for emphasis.

Don't bother learning mysql* functions. They're deprecated because they're insecure. Learning prepared statements is easier than learning to protect against SQL injections, and sooner or later mysql* functions are going away. Hopefully sooner.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1