Retrieve mysql entries from a single array or collection

I've made the following sql query

$dates = mysql_query("SELECT DISTINCT `saledate` FROM `phoneappdetail`");

Now I want to echo results in the array like so.

$dates[0]

I don't want to use a while statement so I can use $date[1], $date[2], ect. in different places on the page.

Answers


You still have to use a while loop in order to traverse the result because mysql_query returns a resource and not an array.

function getResultArray() {
    $result_array =  array();
    $result_set = mysql_query("SELECT DISTINCT `saledate` FROM `phoneappdetail`");

    while ( $row = mysql_fetch_array($result_set) ) {
        $result_array[] = $row;
    }

    return $result_array;
}

$date = getResultArray();
echo $date[0]['saledate'];

You'd need to pivot to do that and read all the dates back in one record

Use a while loop read them into an array, then reference that it will be a heck of a lot easier.


look at the mysqli_fetch_array() function

http://www.php.net/manual/en/mysqli-result.fetch-array.php


Need Your Help

.htaccess redirect all subdirectories to root (Kinda)

.htaccess mod-rewrite redirect

This is not as straight forward as the title might imply. I'll try to explain.

Delete Duplications When Importing

vba import excel-2010

I'm doing this code for the company where I work as a internship. I did some part of it with the help of people from this forum and others but the code is big and I cannot find a place or the piece...