Hello Experts,
I am getting unwanted result.Please help!
Here is the function:

function ArrayResults($result){
		
		while ($row = mysql_fetch_assoc($result)){
		//echo $row["first_name"];	//tested :works!!
                   $this->arr2[] = $row;
                    
		}
		return $this->arr2;
	}

I called the function and used the print_r to see how is the array:

$mysql->ArrayResults($mysql->ExecuteSQL("SELECT * FROM Employee"));
print_r($mysql ->arr2);

Array look weird:

Array ( [0] => Array ( [id] => 1 [first_name] => Jack [last_name] => Schrvaski [start_date] => 1999-07-20 [end_date] => 2011-07-21 [salary] => 1224.56 [city] => Toronto [description] => Programmer ) [1] => Array ( [id] => 2 [first_name] => David [last_name] => Moron [start_date] => 1999-07-20 [end_date] => 2011-07-21 [salary] => 1224.56 [city] => Toronto [description] => Designer ) [2] => Array ( [id] => 3 [first_name] => Eric [last_name] => Ron [start_date] => 1998-08-29 [end_date] => 2011-03-21 [salary] => 1114.56 [city] => Toronto [description] => Tester ) [3] => Array ( [id] => 4 [first_name] => Frost [last_name] => Robert [start_date] => 1999-01-10 [end_date] => 2011-07-10 [salary] => 2114.56 [city] => Toronto [description] => Manager ) [4] => Array ( [id] => 5 [first_name] => Robin [last_name] => Wood [start_date] => 2009-12-22 [end_date] => 2012-02-13 [salary] => 1000.00 [city] => Thronhill [description] => System Analyst ) )

I tried to get a single data using:

print_r($mysql ->arr2[1][2]);

shows no output.But if i type :

print_r($mysql ->arr2[1]['first_name']);

I get David.My question is how to set the array so that I can use loop to get output.
like:

print_r($mysql ->arr2[$i][$j]);

Please help.

Member Avatar for diafol

Use array_values() to change associative array to indexed array.

$ind = array_values($mysql->arr2[$i]);

loop over $ind

Print_r can only show dump values.
You can try following code to show values on screen in better manner.

<?php 
echo "<table>";
echo "<tr>";

echo "<td>ID</td>";
echo "<td>First Name</td>";
echo "<td>Last Name</td>";
   
echo "</tr>";
for ($i=0;$i<count($mysql->arr2);$i++)
{
   echo "<tr>";

   echo "<td>{$mysql->arr2[$i]['id']}</td>";
   echo "<td>{$mysql->arr2[$i]['first_name']}</td>";
   echo "<td>{$mysql->arr2[$i]['last_name']}</td>";
   
   echo "</tr>";
}
echo "</table>";
?>

or still you want to use print_r, then use following code

<?php 

for ($i=0;$i<count($mysql->arr2);$i++)
{

   echo "<pre>";   
   print_r($mysql->arr2[$i]);
   echo "</pre>";
}

?>

Thanks guys.It was really helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.