Reputation:
I'm experiencing a strange issue while running a mysql query from php.
I have a mysql database with many (23) tables; two of them are Users and FollowersList.
FollowersList contains two colums: in the first one there's the code of a user, and in the second one the code of the user that the first one is following; each column references the primary index of Users table.
I have defined a stored procedure called "getFollowings" that returns the codes of the users that someone is following; it is defined in this way:
CREATE PROCEDURE getFollowings(IN cod INT(11))
BEGIN
SELECT Code2 FROM FollowersList WHERE Code1=cod;
END
When I call the stored procedure from phpmyadmin, everything works fine; I get all the correct results.
When I call it from php, using this code:
$sql0="CALL getFollowings('".$cod."')";
$res0=mysqli_query($con, $sql0);
$array0=mysqli_fetch_array($res0);
mysqli_next_result($con);
I can't get the correct results. The connection is defined correctly, and all the variables are correctly defined.
Let's make an example. I'm working on the user with code 94; when I run the procedure from phpmyadmin, I get two results: 63 and 89, which is correct in my database.
If I try the same from a php script, I get an array of dimension 2, but with only the first value; the var_dump is:
array(2) { [0]=> string(2) "63" ["Code2"]=> string(2) "63" }
This means that I receive and array with size=2, but with only one element stored. Do you have any idea why?
Upvotes: 1
Views: 187
Reputation: 2625
You are not iterating the mysqli_fetch_array. Use like this
while($row = mysqli_fetch_array($res0)){
$array0[] = $row;
}
print_r($array0);
and you are using mysqli_fetch_array which give both associative array and numeric array.
If you want associative array use
mysqli_fetch_array($res0,MYSQLI_ASSOC);
If you want numeric array use
mysqli_fetch_array($res0,MYSQLI_NUM);
as default it return both
use like this
while($row = mysqli_fetch_array($res0)){
$array0[] = $row;
}
echo count($array0);
foreach($array0 as $value){
echo $value[0];
}
Upvotes: 3