0

pretty straight forward question this - I am trying to create an array to store the Model and Cost values taken from my database table. I figured I could begin the array, then create a while loop, and then end the array, and smiles all around. I may be mistaken, or I may have blindly missed something in my code, but could you have a look?

$array = array(
    while ($overall_cost = mysql_fetch_assoc($query_ocost)) {
            $overall_cost["model"] => $overall_cost["cost"],
    }
);

var_dump($array);
4
  • Have you tried running this? It's syntactically wrong... Commented Dec 10, 2012 at 15:55
  • 1
    Not just syntactically. This is wrong in many other ways. Commented Dec 10, 2012 at 15:55
  • 2
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Dec 10, 2012 at 16:02
  • Of course it's wrong, otherwise I would have not posted for help. This site isn't about boasting skill, I am clearly unaware of how to do this and wanted your input. Thanks NullPointer for your resources. I'll take a look. Commented Dec 11, 2012 at 10:29

3 Answers 3

3

I think this is what you're looking for:

$array = array();

while ($overall_cost = mysql_fetch_assoc($query_ocost)) {
    $array[$overall_cost["model"]] = $overall_cost["cost"];
}

var_dump($array);
Sign up to request clarification or add additional context in comments.

1 Comment

Great, that makes sense now I see it. This will come in handy in the future too, thanks.
1

You can't do it like this. You need to add to the array inside the while loop:

$array = array();
while ($overall_cost = mysql_fetch_assoc($query_ocost)) {
        $array[$overall_cost["model"]] = $overall_cost["cost"];
}    

var_dump($array);

would be one way of doing it.

EDITED to produce simple array.

3 Comments

Which is how the OP had it (albeit wrongly) in his post. I don't think it was worth a downvote.
Actually the OP didn't have it as a multi-dimensional array. If PHP worked like he thought, it would have created this: $array = array(something => something, something => something, ); I didn't vote you down though.
I didn't spend hours figuring this one out, I simply tried what I thought would work with little experience in creating arrays. My example gave you a clear understanding of what I was trying to achieve. Thanks for your response GarethL.
1

I don't think that will work. Try something like:

$array = array();
while ($overall_cost = mysql_fetch_assoc($query_ocost)) {
      $array[$overall_cost["model"]] = $overall_cost["cost"];
}


var_dump($array);

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.