简体   繁体   English

使用PHP将Yahoo Finance中的csv导入MySQL

[英]Import csv from Yahoo Finance into MySQL using PHP

Using Johnboy's tutorial on importing .csv files to MySQL, I tried to make a script that would take exchange rate data from Yahoo finance, and write it to the MySQL database. 使用Johnboy的教程将.csv文件导入MySQL,我尝试制作一个脚本,从雅虎财务中获取汇率数据,然后将其写入MySQL数据库。

<?php  

//connect to the database 
$host = "****"
$user = "****"
$password = "****"
$database = "****"
$connect = mysql_connect($host,$user,$password); 
mysql_select_db($database,$connect);

//select the table  
if ($_FILES[csv][size] > 0) { 

    //get the csv file 
    $symbol = "ZARINR"
    $tag = "l1"
    $file = "http://finance.yahoo.com/d/quotes.csv?e=.csv&f=$tag&s='$symbol'=x"; 
    $handle = fopen($file,"r"); 

    //loop through the csv file and insert into database 
    do { 
        if ($data[0]) { 
            mysql_query("INSERT INTO ZAR_to_INR(exchange_rate) VALUES 
                ( 
                    '".addslashes($data[0])."', 
                ) 
            "); 
            echo "done";
        } 
    } while ($data = fgetcsv($handle,1000,",","'")); 

    //redirect 
    header('Location: import.php?success=1'); die; 

} 

else{
    echo "nope";
}

?> 

I added the echos in the hope that they'd tell me whether or not the script worked. 我添加回声,希望他们告诉我脚本是否有效。 It doesn't work at all. 它根本不起作用。 There are no error messages or anything. 没有错误消息或任何内容。 When I run the script by opening it in my webhost, it simply does not run. 当我通过在我的webhost中打开它来运行脚本时,它根本就不会运行。

I'd appreciate any advice on how to make this script work (or even an alternate way of solving the problem). 我很欣赏有关如何使这个脚本工作的任何建议(甚至是解决问题的另一种方法)。

try using mysql debugs : 尝试使用mysql调试:

mysql_select_db($database) or die('Cant connect to database'); 
$result = mysql_query($query) or die('query fail : ' . mysql_error());
$connect = mysql_connect($host,$user,$password) 
    or die('Cant connect to server: ' . mysql_error());

to find this outputs you need to check your php error_log : where-does-php-store-the-error-log 找到这个输出你需要检查你的php error_log: where-does-php-store-the-error-log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM