0

Greeting ,

I am using an ODBC connection with qucikbooks. Using PHP i can show quickbooks data on my website. Now I would like to save that data in my local MYSQL. So I create an table and some fields.

//this line gets data from quickbooks connection
$query = odbc_exec($conn, "SELECT GivenName FROM Customers");

//fetch data in an array
while($row = odbc_fetch_array($query)){

    //Go through the array and save the data into MYSQL.
    foreach ($row as $key => $value) {
        echo  $value . "<br>";

        //Insert data into mySQL. 
        $sql = "INSERT INTO Customers (GivenName) VALUES ('$value') ";
        if (mysqli_query($mysqlconn, $sql)) {
            echo "New record created successfully";
        }
    }
}

The foreach loop above dont work.

My question is how I can use foreach loop to get get the data from an array, and insert it into mysql table; and also dont insert any empty field.

Many Thanks

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • doesn't make any sense at all. You are selecting from Customers and inserting again into Customers – e4c5 Jan 11 '17 at 16:23

2 Answers2

0

Try this :

function get_data_from_cloud(){
$conn=odbc_connect('CLOUD','','');
if (!$conn) {
    exit("Connection Failed: " . $conn);
}
$sql="SELECT DATETIME, NAME, CNDROP 
      FROM TABLE1 
      WHERE DATETIME>='2014-09-28 00:00:00' and 
            DATETIME<='2014-09-28 23:00:00' and 
            NAME IN ('PETER') 
      GROUP BY DATETIME, NAME 
      ORDER BY DATETIME, NAME";

$result=odbc_exec($conn,$sql)or die(exit("Error en odbc_exec"));

$data = array();
while (odbc_fetch_row($result)) {
    $data[]=array('DATETIME' => odbc_result ($result, "DATETIME"), 
                  'NAME'=> odbc_result ($result, "NAME"), 
                  'CNDROP'=> odbc_result ($result, "CNDROP"));
}
return $data;   
}

ODBC/MYSQL Insert a Query Result from ODBC to a databse in MYSQL

i hope it's help you

Community
  • 1
  • 1
Sujal Patel
  • 2,444
  • 1
  • 19
  • 38
  • 3
    Your answer is **no way** related to OP's question. If you want to give an answer, please stick to the question at hand. – Rajdeep Paul Jan 11 '17 at 16:00
  • Do or do not. There is no "try". A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Apr 07 '17 at 13:55
0

I have understood what you doing. But I cannot see an Insert statement anywhere. After getting the data from cloud; I would like to loop through that data and save in MYSQL table on my local server.