1

Maybe I have long since gotten to used to using ORM methods to pull data from a DB but I am now working up a simple little project for a buddy of mine who doesn't need all the extra guff of MVC's, ORM's and so on.. With that below is an example of the query I am putting together in a class I am building up in PHP.

$this->sqlstart();
$sql = "select label, setting from ".$this->_settings." where id <= 4";
$query = mysql_query($sql);

$result = array();
while($row = mysql_fetch_array($query))
{
    $result[] = $row;
}
return $result;

The output from return $result is:

Array
(
    [0] => Array
        (
            [0] => Week 1 Pay
            [label] => Week 1 Pay
            [1] => 2995
            [setting] => 2995
        )

    [1] => Array
        (
            [0] => Week 2 Pay
            [label] => Week 2 Pay
            [1] => 2995
            [setting] => 2995
        )

    [2] => Array
        (
            [0] => Week 1 Dates
            [label] => Week 1 Dates
            [1] => 1-15
            [setting] => 1-15
        )

    [3] => Array
        (
            [0] => Week 2 Dates
            [label] => Week 2 Dates
            [1] => 16-31
            [setting] => 16-31
        )

)

And it should be

Array
(
    [0] => Array
        (
            [label] => Week 1 Pay
            [setting] => 2995
        )

    [1] => Array
        (
            [label] => Week 2 Pay
            [setting] => 2995
        )

    [2] => Array
        (
            [label] => Week 1 Dates
            [setting] => 1-15
        )

    [3] => Array
        (
            [label] => Week 2 Dates
            [setting] => 16-31
        )

)

can anyone point out to me where the additional data in the sets is coming from?

3
  • Check the manual for mysql_fetch_array(). php.net/manual/en/function.mysql-fetch-array.php Commented Mar 30, 2013 at 23:16
  • @MichaelBerkowski I think you are right. I should have searched a bit harder. I would have likely stumbled across the answer Commented Mar 30, 2013 at 23:22
  • PS, this sounds like a GREAT opportunity to get used to using the mysqli or PDO apis instead of the obselete mysql one! Commented Mar 30, 2013 at 23:23

4 Answers 4

4

You should use mysql_fetch_assoc to get the result properly. It will provide you an associative array instead of a normal one.

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

Comments

2

From http://php.net/manual/en/function.mysql-fetch-array.php

array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )

Either pass MYSQL_ASSOC as the second param to just get an associative array back, or just call mysql_fetch_assoc().

1 Comment

Yea, I think its safe to say I am very much used to using ORM methods now, can't believe I forgot so much with going the straight mySQL methods that php has to offer. Guess this will serve as a good refresher project. Thanks :-)
2

You should be able to pass in a second argument which will only output the associative column names:

while($row = mysql_fetch_array($query, MYSQL_ASSOC))

1 Comment

or use the mysql_fetch_assoc(...) method.
0

mysql_fetch_array returns two ways to access values: field name and index number. You might change mysql_fetch_array to mysql_fetch_assocor change the code to:

while($row = mysql_fetch_array($query)){
     $result[] = array('label' => $row['label'], 'setting' => $row['setting'];
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.