1

I am new to PHP and hope someone can help me with this.

I have a Select query that returns (among others) the values from two columns in a db table, "ID" and "en". When getting the results from the Select I would like to store both these values for each item in a two dimensional array.

So far I have the below. The Select itself works correct but when pushing to the array it only outputs the last item from the Select results (for which it looks correct as well) so my guess is instead of adding each item to the array I am overwriting it every time.

Can someone tell me what I am doing wrong here ? Also, please let me know if something other than While should be preferred here.

My PHP:

<?php     
    $tbl = "TranslationsMain";  

    $conn = new mysqli($servername, $username, $password, $dbname);
    if($conn->connect_error){
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT * FROM " . $tbl;
    $result = $conn->query($sql);   
    while($translations = $result->fetch_assoc()){
        $arr = array('ID' => $translations['ID'], 'en' => $translations['en']);
    }
    $conn->close();
    print_r($arr);
?>

Current output (tested with a small table):

Array ( [ID] => ID10 [en] => Value10 )

Expected output:

Array ( [ID] => ID1 [en] => Value1, [ID] => ID2 [en] => Value2, [ID] => ID3 [en] => Value3, ... [ID] => ID10 [en] => Value10  )

Many thanks in advance, Mike

2 Answers 2

1

You are getting this output because you are overwriting the keys in your array. You would need to save to another array (aka not 2-dimensional):

<?php     
    $tbl = "TranslationsMain";  

    $conn = new mysqli($servername, $username, $password, $dbname);
    if($conn->connect_error){
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT * FROM " . $tbl;
    $result = $conn->query($sql);   
    while($translations = $result->fetch_assoc()){

        // ***** Create a new array here ***** //
        $arr[] = array('ID' => $translations['ID'], 'en' => $translations['en']);

    }

    $conn->close();
    print_r($arr);
?>

Gives you:

Array ( 
        [0] => Array ([ID] => ID1 [en] => Value1 ),
        [1] => Array ([ID] => ID2 [en] => Value2 ),
        [2] => Array ([ID] => ID3 [en] => Value3 ),
      )
Sign up to request clarification or add additional context in comments.

5 Comments

This works perfect ! Thanks so much - I was fighting with this for quite a while today. :) Before I accept the answer just two final questions since I am new to this, hope that's ok ? 1) Is While a good approach here or would you suggest anything different instead ? 2) Can you also show me how to echo a specific item from this array ?
Nope, this is the way to store multiple rows, you were close though with what you had!
You want to access a row after creating it? You just do echo $arr[0]['ID']; for ID then en for the other.
Awesome. :) Thanks again - this really is a huge help !
No problem. We all gotta start somewhere! Cheers.
1

Replace $arr to $arr[] and then try.

Comments

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.