Hi, don't know what's wrong, when i try to dispay an array, nothing is displayed.

// class.php
class Connection{
var $arr=array();
function getData($username){
	$q="select comment_id from comments where user='$username'";
	$result=$this->conn->query($upit);
        $n=$result->num_rows;
	for($i=0; $i<n; $i++){			
		$this->arr[$I]=$result->fetch_array[MYSQLI_ASSOC];  // I've also tried $this->arr[] (with no index) but the same thing
	}
	return $this->arr;
}	
}

and here i call function

// data.php

$data=$Connection->getData($user);     // count($data) is > that 0
foreach($data as $d){           
       echo $d.'<br />';                        // nothing is displayed
}

Could someone resolve this or post some example of 1D array that is received from function. Thanks

From what I can see you had actually have a two dimentional array there.
Try the following to see what getData is returning

<?php

print_r($Connection->getData($user));

?>
print_r($Connection->getData($user));

gives (4 rows as it should):

Array ( [0] => [1] => [2] => [3] => )

If that is the actual output from the print_r function, the array is being returned with 4 values, all empty or something similar.

Convert the binary data returned into hex and see if something is being returned from the sql query that should not be.

echo bin2hex($d);

echo bin2hex($d); - nothing is displayed

Array ( [0] => [1] => [2] => [3] => ) from print_r function means that values are empty, but the same query done directly in phpMyadmin returns not empty values

Echo results directly in the getData() function.

$q="select comment_id from comments where user='$username'";	$result=$this->conn->query($upit);

Whats upit?

for($i=0; $i<n; $i++){					
   $this->arr[$I]=$result->fetch_array[MYSQLI_ASSOC];  
}

Why have you used "$I" instead of "$i"?

Since the $d array values are empty, is the getData() function written properly?

$result=$this->conn->query($q);
$n=$result->num_rows;
for($i=0; $i<n; $i++){
    $this->arr[$i]=$result->fetch_array;
}

That looks better.

@omol, mistakes here on forum, sorry
should be $i, and $q. In original its different so I've adjusted for English language

Just to say that i solved this problem; instead of searching for one value in query (comment_id) it worked with multiple arrays when i made query with more values (comment_id, user, time, etc.).
Thanks for help anyway!

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.