-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?

4
  • 7
    What's the problem? What's not working? What does that print_r print? Commented Feb 22, 2013 at 18:48
  • 4
    What are you trying to accomplish, and what's happening instead? Commented Feb 22, 2013 at 18:48
  • 3
    You asked a question but didnt state the question... Commented Feb 22, 2013 at 18:49
  • 9
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. Commented Feb 22, 2013 at 18:49

2 Answers 2

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

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

1 Comment

These are good suggestions, but the original code should still work as-is.
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.

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.