I'm using this PDO query to fetch all the data I need from Value and dateid:

$pdo = new PDO('mysql:host=localhost;dbname=contisec_portal', 'root', '');
        $date = date("o-m-d");
        $stmt = $pdo->query("SELECT stock_names.Value,stock_names.Fileid,date_header.dateid FROM stock_names,date_header WHERE SEDOL = '$stocksel' AND date_header.fileid = stock_names.FileID");

Does anybody know how I can sort the two columns into their own arrays (i.e. $valuearray and $dateidarray)?

Many Thanks,

Ed

Recommended Answers

All 6 Replies

Member Avatar for diafol

Maybe:

$stmt = $pdo->query("SELECT stock_names.Value,stock_names.Fileid,date_header.dateid FROM stock_names,date_header WHERE SEDOL = '$stocksel' AND date_header.fileid = stock_names.FileID");

$values = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
$dates = $stmt->fetchAll(PDO::FETCH_COLUMN, 2);

Top of head, not tested.

Do you know how I can print out all the values so I can test to see the array is being filled?

Member Avatar for diafol
print_r($values);

As these questions are pretty rudimentary, how about looking at some tutorials or the php.net manual?

Ok, I've tested it out and it works with the first array ($values) but nothing displays for dates.
Any ideas?

Thanks,

Ed

Member Avatar for diafol

Sorry, my mistake - the first fetchAll has come to the end of the records so there's nothing left to fetch. Noob mistake! Why not use a simple while loop?

$values = array();
$dates = array();
while($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $values[] = $data['value'];
  $dates[] = $data['date'];
}

Great, everything works now.
Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.