Hello I can not figure out how to access a two dimensional array returned by a function.
My function query($query)
inside a Connection class(inside Connection.php
):
public function query($query)
{
$result = mysql_query($query);
$array= array();
for($i=0; $i<mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
for($j=0;$j<(sizeof($row)/2);$j++)
{
$array[i][j]=$row[$j];
}
}
return $array;
}
and My test.php file:
<?php
include 'Connection.php';
$obj1 = new Connection();
echo $obj1->toString();
$array=$obj1->query("SELECT * FROM LeCars ORDER BY ID;");
echo $array[0][0];
echo 'asd:',$array[0][0],'<br>';
echo 'asd:',$array[0][1],'<br>';
echo 'asd:',$array[0][2],'<br>';
echo 'asd:',$array[1][0],'<br>';
echo 'asd:',$array[2][4],'<br>';
var_export($array);
?>
output of test.php:
ip : 194.00.00.27 login: asd123 password: asd123 table: asdasd1123 query: asd: asd: asd: asd: asd: array ( 'i' => array ( 'j' => NULL, ), )
Any1 knows how to send that array into test.php properly ?
What I tried: looping it, sending by refference, double checked if array rly works(it does), but I cant send it to test and access it there
mysql_query() does not return 2-dimensional array. It return 'resource' which means it is noa t a php type object. You can operate on it with special function.
See docs and examples right here: http://php.net/manual/en/function.mysql-query.php
First of all, toString() should only return that string value, not print it.
Then, two-diumensional arrays returned from function are just like normal two-dimensional arrays - haven't you tried:
$array=$obj1->query("SELECT * FROM LeCars ORDER BY ID;");
echo $array[0][0];
?
Also please consider reading the PHP manual for "foreach" and "count".