0

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

3
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by newprimitive
0

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));

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

gives (4 rows as it should):

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

0

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);
0

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

0

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"?

0

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

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

0

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.