Hello, $query2 = mysql_query("SELECT system_id FROM `dev_attach_systems` WHERE dev_id = '$dev_id' "); $systemsArray = mysql_fetch_array($query2); print_r($systemsArray); PHP: In DB I have 2 rows that match this query, but for some reason when I PRINT_R the Array I see only 1 : Array ( [0] => 6 [system_id] => 6 ) Why is that? I'm asking because latter I do if (in_array($sys_id, $systemsArray)) PHP: And I get only 1 result...:/
mysql_fetch_array returns an array of a single row. After it is accessed, it increments to the next result in the set. You basically need to loop through the results. Replace: $systemsArray = mysql_fetch_array($query2); print_r($systemsArray); With: while($systemsArray = mysql_fetch_array($query2)) { print_r($systemsArray); }
What are you trying to do? Looping through mysql_fetch_array would roughly be equivalent to looping through an array. Is there something specific you need to do? You can create a separate array by looping through the results, and setting values each loop, however it's redundant since you would normally be looping through an array in the same manner.
I'm trying to create array with all the DB results I receive from my SQL query. Each cell contain one result. For example: $systemsArray = 21 ; 5 ; 34 ; 17 mean - in the first cell of Array "$systemsArray" I have the first SQL result - "21", in the second I have "5"... After I have the full Array I have another script code (in other part of my page/source) that check if some number is in the Array "$systemsArray" This is what I'm trying to do. So, How can I put all the result in Array "$systemsArray"? I thought that "$systemsArray = mysql_fetch_array($query2)" already do that for me...
Here's what you should do. This is sort of redundant, but should give you the output you are looking for. $systemsArray = array(); $query2 = mysql_query("SELECT system_id FROM `dev_attach_systems` WHERE dev_id = '$dev_id' "); while ($output_array = mysql_fetch_array($query2)) { $systemsArray[] = $output_array['system_id']; } print_r($systemsArray); PHP: