Jeff Janes
Jeff Janes

Reputation: 895

Loading mysql calls into arrays

Normally have not had to do this alot but my process didn't seem to work correctly this time so I am asking for some clarification

This works for a single item in the query:

    $query="SELECT data1 as product FROM Products ORDER BY Title"; 
    $result = mysql_query($query)
        or die("Query failed -".$query);        
    $rows=array();
    while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
        $rows[] = $row;
    }

As I said this works fine for a single item being returned all of the items returned in the query end up in an array called $rows

    $query="SELECT data1 as product, data2 as info FROM Products ORDER BY Title"; 
    $result = mysql_query($query)
        or die("Query failed -".$query);        
    $i=0;
    $data1=array();
    $data2=array();

    while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
        $data1[$i] = $row[0];
        $data2[$i++] = $row[1];
    }

This does not seem to do the same thing or I missed something.

Am I missing something? If not then I need to look at something else as my issue but need to make sure about this.

The result I desire is two arrays one containing the all of first items from the query result and the second array containing all of the second items from the query result.

Upvotes: 0

Views: 95

Answers (2)

Matheus Azevedo
Matheus Azevedo

Reputation: 908

Try:

$row = array();
while($row[] = mysql_fetch_array($result, MYSQL_BOTH)) {}

And, as already stated, use mysqli or PDO. mysql_* has been deprecated.

Upvotes: 2

Tom
Tom

Reputation: 173

 $query="SELECT data1 as product, data2 as info FROM Products ORDER BY Title"; 
$result = mysql_query($query)
    or die("Query failed -".$query);        
$i=0;
$data1=array();
$data2=array();

while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    $data1[$i] = $row[0];
    $data2[$i] = $row[1];
    $i++;
}

Upvotes: 0

Related Questions