DB results

Discussion in 'PHP' started by roice, Nov 16, 2011.

  1. #1
    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...:/
     
    roice, Nov 16, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    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);

    }
     
    jestep, Nov 16, 2011 IP
  3. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Can't I receive array with all the reaults (system_id) that my query found?
     
    roice, Nov 16, 2011 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    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.
     
    jestep, Nov 16, 2011 IP
  5. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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...
     
    roice, Nov 16, 2011 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    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:
     
    jestep, Nov 16, 2011 IP
  7. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks!
    ......
     
    roice, Nov 17, 2011 IP
  8. akbarul

    akbarul Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    How awesome is that, let's do installing that. Anyway thanks for sharing.
     
    akbarul, Nov 23, 2011 IP