0

I wrote a script to download a google sheet, and then upload this sheet to my dadabase.

Everything seems to be working fine except that the data is now being uploaded to my database.

I navigated to the folder and then ran this command:

php -f updatedatabase.php

This is the script I wrote (the googel sheet key is filled in with the actual key--i tested the link and it is working, and the database credentials have been starred out for security reasons):

<?php
    $datas = file_get_contents("https://docs.google.com/spreadsheets/d/1fOe2fz8PmYzTWU7l_KboHdv0zP0KZkhcJUTGJDUXaKk/gviz/tq?tqx=out:csv&sheet=All%20Items&range=A:G");
    $datas = explode( PHP_EOL, $datas );
    //FORMAT ("Name1", "Image1", "Description1", "Rarity1", "Price1", "Status1", "Store1"), ("Name2", "Image2", "Description2", "Rarity2", "Price2", "Status2", "Store2").....

    // Info to connect to the database
        $servername = ".com";
        $dbusername = "";
        $password = "!19";
        $dbname = "";

        $con=mysqli_connect( $servername, $dbusername, $password, $dbname );

    mysqli_query( $con, "TRUNCATE TABLE Items" );

    foreach ( $datas as $data ) {
        echo $data . "\n";
        mysqli_query( $con, "INSERT INTO Items (Name,Image,Description,Rarity,Price,Status,Store) VALUES " . $data );
    }

    mysqli_close($con);
?>
8
  • Can you show the log? Commented Aug 19, 2018 at 4:08
  • Do you mean my php error log or the output from running the script? Because honestly I can't find the error logs... Commented Aug 19, 2018 at 4:17
  • Please try godaddy.com/help/working-with-error-logs-1197 Commented Aug 19, 2018 at 4:19
  • 1
    I did that and I don't have the account level necessary. I have the error logs though. It seems that there was an unexpected EOF. Thanks I should have thought of that in the first place. I was expecting errors to be output to the console like ubuntu Commented Aug 19, 2018 at 4:20
  • Okay it doens't seem to like me using the $data returned from downloading the CSV as the file to upload. Commented Aug 19, 2018 at 4:27

1 Answer 1

1

Your data string is not well formated:

$csvData = file_get_contents("https://docs.google.com/spreadsheets/d/1fOe2fz8PmYzTWU7l_KboHdv0zP0KZkhcJUTGJDUXaKk/gviz/tq?tqx=out:csv&sheet=All%20Items&range=A:G");
$lines = explode(PHP_EOL, $csvData);
$array = array();
foreach ($lines as $line) {
    $array[] = str_getcsv($line);
}

// format the data string
// work with PHP7.0 or later
$datas = array_map(function ($data) {
    $dataString = implode("','", $data);
    return "('$dataString')";
}, $array);

// remove the head line
array_shift($datas);

// Info to connect to the database
$servername = "192.168.10.10";
$dbusername = "homestead";
$password = "secret";
$dbname = "gooledoc";

$con = mysqli_connect($servername, $dbusername, $password, $dbname);

mysqli_query($con, "TRUNCATE TABLE Items");

foreach ($datas as $data) {
    echo $data . "\n";
    $result = mysqli_query($con, "INSERT INTO `Items` (`Name`,`Image`,`Description`,`Rarity`,`Price`,`Status`,`Store`) VALUES " . $data);
    var_dump($result);
}

mysqli_close($con);
Sign up to request clarification or add additional context in comments.

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.