0

I'm trying to store my mysql query in an array like this :

$arr = array ( "field1"=>"value" , "field2"=>"value" , "field3"=>"value" , ... );

I've tried this but don't work like I need :

$row_selectProduct = array();
$sResult = $db->query( 'SELECT * FROM tbl' )    

while ( $aRow = $sResult->fetch_assoc() )
{
        $sInfo = $sResult->fetch_field();
        // General output
        $row_selectProduct[ $sInfo->name ] = $aRow;
}

$sResult->close();

Thx for help

EDIT : Sorry for misunderstanding... I'd like to name keys by the field name in database. I try to reproduce this :

$result = array();

while ($row_selectProduct = mysql_fetch_assoc($selectProduct));
{

    $row_array= array();

$row_array['field_name1_In_DB'] = $row_selectProduct['field_name1_In_DB'];
$row_array['field_name2_In_DB'] = $row_selectProduct['field_name2_In_DB'];
$row_array['field_name3_In_DB'] = $row_selectProduct['field_name3_In_DB'];
$row_array['field_name4_In_DB'] = $row_selectProduct['field_name4_In_DB'];
$row_array['field_name5_In_DB'] = $row_selectProduct['field_name5_In_DB'];
...

array_push($result,$row_array);

};
7
  • 3
    don't work like i need doesn't tell much. What's the current result? Commented May 23, 2013 at 16:12
  • Is that example supposed to be one row or several rows? Isn't $aRow already in the format you want? Commented May 23, 2013 at 16:16
  • @deceze SELECT * FROM tbl will definitly result in several rows Commented May 23, 2013 at 16:20
  • @MrSo If it's the current result - you've got what you want. What's the problem? Commented May 23, 2013 at 16:20
  • @steven Not if there's only one row. ;-P $arr Seems to be only one row though, so if that's the desired output the question makes no sense. Well, it doesn't either way. Commented May 23, 2013 at 16:21

2 Answers 2

1

Using PDO connection functions, it's quite simple --

foreach($db->query('SELECT field_name1,field_name2 FROM table') as $row) {
    echo $row['field_name1'].' '.$row['field_name2']; //etc...
}

Here's a good tutorial for PDO in PHP if you want more.

Sign up to request clarification or add additional context in comments.

Comments

0
while ( $aRow = $sResult->fetch_assoc() )
{
        foreach($aRow as $fieldname => $fieldvalue) {
             $row_selectProduct[$fieldname] = $fieldvalue;   
        }

}

but you will overwrite your $row_selectedProduct with every record and the var at the end will only have stored the last record

if you would like to have all results in one array you should:

$results = array();
while ( $aRow = $sResult->fetch_assoc() )
{
        $results[] = $aRow;

}

if you know that there will be only one result you need only:

$result = $sResult->fetch_assoc();

3 Comments

The inner loop is completely superfluous.
$rowSelectProduct[] = $aRow;.. no need to waste a bunch of cpu cycles looping over the result row array when you can simply assign the entire array.
yes I know but i want to make it clear what would be the right syntax.

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.