-2

I'm having trouble with this script and I would appreciate any here:

<? php

//ini_set('display_errors', '1');

//MQSQL connection
require_once('../../include/init.phph');

function transportQueue(){
//siteinfoDbCon();
siteinfoDbConNoLag();

$query = "SELECT 
        a.transport_id, a.site_id, b.site_name, c.name as 'From DC', 
        d.name as 'To DC', a.cutover, a.working, a.error 
        FROM transport_queue a 
        INNER JOIN sites b ON(a.site_id=b.site_id) 
        INNER JOIN datacenters c ON(a.from_dc_id=c.datacenter_id) 
        INNER JOIN datacenters d ON(a.to_dc_id=d.datacenter_id) 
        WHERE 
        a.cutover BETWEEN NOW() AND ADDDATE(NOW(), INTERVAL 7 day) 
        ORDER BY a.cutover";

//Pull results
$results=mysql_query($query);

//Return into an array $transports
while($row=mysql_fetch_array($results,MYSQL_BOTH)){
  $transports[]=$row;
  print_r($transports[0]["site_id"]);
}

//Free up the results 
mysql_free_result($results);

//Close the db connection
siteinfoDbClose();

}
transportQueue();
?>

I can confirm that the query works and the DB connections work... I'm at a loss.

I'm trying to return the results into an associative array. I did review the following question/answer: Dump mysql_fetch_array results into a multidimensional array

Trying to

Any thoughts?

Community
  • 1
  • 1
Alan Williams
  • 35
  • 1
  • 8
  • 7
    What's the problem? What's not working? What does that `print_r` print? – gen_Eric Feb 22 '13 at 18:48
  • 4
    What are you trying to accomplish, and what's happening instead? – DiMono Feb 22 '13 at 18:48
  • 3
    You asked a question but didnt state the question... – Nick Feb 22 '13 at 18:49
  • 9
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. – Kermit Feb 22 '13 at 18:49

2 Answers2

1

This

while($row=mysql_fetch_array($results,MYSQL_BOTH)){
  $transports[]=$row;
  print_r($transports[0]["site_id"]);
}

Replace this

$transport = array();
while($row=mysql_fetch_assoc($results))
  $transports[]=$row;

print_r($transports);

And it's time to move to MySQLi http://www.php.net/manual/en/book.mysqli.php

Winston
  • 1,758
  • 2
  • 17
  • 29
0

This line:

print_r($transports[0]["site_id"]);

...is always looking at the single value ['siteid'] in the first row of transports, rather than the array. Pull it out, and then after the loop is over run this:

print_r($transports);

...and you should get the results you're after.

DiMono
  • 3,308
  • 2
  • 19
  • 40