PHP/JSON not working
Posted: Thu Aug 13, 2009 9:59 am
I think I'm getting the array conversion wrong. I am trying to copy the results of the query from one array to another. However I am not sure how to assign name/value pairs into the array. Code below:
Code: Select all
<?php
$myServer = "myserver.com";
$myUser = "user";
$myPass = "pass";
$myDB = "Northwind";
//create an instance of the ADO connection object
$conn = new COM ( "ADODB.Connection" ) or die ( "Cannot start ADO" );
//define connection string, specify database driver
$connStr = "PROVIDER=SQLOLEDB;SERVER=" . $myServer . ";UID=" . $myUser . ";PWD=" . $myPass . ";DATABASE=" . $myDB;
$conn->open ( $connStr ); //Open the connection to the database
//declare the SQL statement that will query the database
$query = "
SELECT * from Employees
";
//execute the SQL statement and return records
$rs = $conn->execute ( $query );
$num_columns = $rs->Fields->Count ();
//echo $num_columns . "<br>";
$returnArray = array();
for($i = 0; $i < $num_columns; $i ++) {
$fld [$i] = $rs->Fields ( $i );
}
for($i = 0; $i < $num_columns; $i ++) {
array_push($returnArray, $fld);
}
//Start the output of XML
while ( ! $rs->EOF ) //carry on looping through while there are records
{
for($i = 0; $i < $num_columns; $i ++) {
array_push($resultarray, $fld [$i]->value);
//$rs->MoveNext ();
}
//move on to the next record
}
echo json_encode($returnArray);
//close the connection and recordset objects freeing up resources
$rs->Close ();
$conn->Close ();
$rs = null;
$conn = null;
?>