0

-----problem solved, see the update 2 below---- I put all MySQL query results into a 2D array:

$suppDescription=mysql_query($query);
$rows = mysql_num_rows($results);
$allSupplierInfo=array();

for($i=0; $i<$rows; $i++){
    $allSupplierInfo[]=mysql_fetch_row($suppDescription);
}

But now I cannot access the $allSuppliersInfo fields.

 echo $allSupplierInfo[1][1]; // prints out 'Array'
 echo $allSupplierInfo[1]['id'];  //prints out nothing

What am I doing wrong?

------- UPDATE-----

print_r($allSupplierInfo) prints the following, so it looks like the loop is not working as I wanted it to:

Array (
    [0] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) ) 
    [1] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) ) 
    [2] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) ) 
    [3] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) ) 
    [4] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] =>     Address_D [3] => Link_D ) [4] => Array ( [0] =>ID_E[1] => Name_E [2] => Address_E [3] => Address_E ) ) ) 

------ UPDATE 2----- Using the while loop, as suggested by RiggsFolly, solved the problem and I can access the fields as I initially wanted. I still do not understand why the for loop I used did not loop as I thought it would - any explanation would be greatly appreciated.

7
  • print_r($allSupplierInfo) print this and see is this printing properly or not. Commented Mar 30, 2015 at 14:30
  • can you show me entire code. Commented Mar 30, 2015 at 14:32
  • May just be a typo on the question, but in the first block of code you have $allSupplierInfo, but on the echo, you have an extra s in the variable name $allSuppliersInfo Commented Mar 30, 2015 at 15:10
  • @JRichardSnape - that was indeed just typos, fixed and well spotted, thanks! Also, added the result of print_r Commented Mar 30, 2015 at 19:03
  • Sorry Isabel you cannot change your question, you are supposed to ask a new one, or the original question and answers will be no use to others seeking help for similiar problems. Commented Mar 30, 2015 at 19:14

1 Answer 1

1

I think you got confused with your mysql result processing, you are using $result when checking for the number of rows returned but it should be $suppDescription.

This means that your for loop will not run as you will be getting 0 or probbaly FALSE as a response to mysql_num_rows($suppDescription);

$suppDescription=mysql_query($query);
$rows = mysql_num_rows($suppDescription);
$allSupplierInfo=array();

for($i=0; $i<$rows; $i++){
    $allSupplierInfo[]=mysql_fetch_row($suppDescription);
}

Also this is easier done with a while loop, then you just process whatever is returned and dont need to bother getting the number of rows.

$suppDescription=mysql_query($query);

$allSupplierInfo=array();

while ( $rows = mysql_fetch_row($suppDescription) ) {
    $allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Riggs, it was a typo in my code, I fetched the same rows twice using with two different variables, $results and $suppDescription. I fixed it but the problem did not change. I'll try the while loop you suggested, looks like my for loop did not loop the way I expected (I updated my original question to reflect that)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.