0

I have a table with 2 columns

first column is a file id (varchar)

second is date (varchar)

I want to fetch these 2 columns for each record in my table and stick it in a assoc array

I am aiming to put the file id as the key and the date as the value

I am a bit stuck at the moment, as I am changing my code from an indexed array to a assoc array.

here is what I have so far.

function getLiveList(){
    $idQuery = "SELECT file_id FROM live_list";
    $dateQuery = "SELECT date FROM live_list";

    $fileid = mysql_query($idQuery);
    $date = mysql_query($dateQuery); 

    $array = array();


    while($row = mysql_fetch_assoc($date)){
        $array[$row] = $date;
    }
    return $array;
}

I know this is wrong but I am on the correct lines im sure!

Here is my error: Warning: Illegal offset type in...

1

2 Answers 2

3

First, use mysqli:

$Link = mysqli_connect("localhost", "my_user", "my_password");
$Query = "SELECT file_id, date FROM live_list";
$Result = mysqli_query($Link, $Query);

while($row = mysqli_fetch_assoc($Result)){
    $array[$row['file_id']] = $row['date'];
}
return $array;

You don't need to duplicate the query for each column, they can be returned in the same query. Each time a row is fetched $row is an associative array with the columns as the keys.

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

Comments

0

try this

 function getLiveList()
 {
        $idQuery = "SELECT file_id FROM live_list";
        $dateQuery = "SELECT date FROM live_list";

        $fileid = mysql_query($idQuery);
        $date = mysql_query($dateQuery); 
        $dates = mysql_fetch_array($date);
        $array = array();

    while($row = mysql_fetch_assoc($fileid))
        {
         $array[$row['file_id']] = $dates['date'];
        }
    return $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.