Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在mysql数据库中获取货币汇率并更新旧汇率_Php_Mysql_Sql_Api - Fatal编程技术网

Php 如何在mysql数据库中获取货币汇率并更新旧汇率

Php 如何在mysql数据库中获取货币汇率并更新旧汇率,php,mysql,sql,api,Php,Mysql,Sql,Api,我厌倦了寻找解决方案,如何获取货币汇率并使用PHP将其存储在我的数据库中。我已成功连接到数据库,但无法更新速率 我有一张叫做股票和货币的桌子 我还有名为[货币][即期汇率][卖出汇率][买入汇率]的专栏 这是一个PHP代码,我正试图改变我的解决方案 class CurrencyConverter { var $xml_file = "www.ecb.int/stats/eurofxref/eurofxref-daily.xml"; var $localhost,

我厌倦了寻找解决方案,如何获取货币汇率并使用PHP将其存储在我的数据库中。我已成功连接到数据库,但无法更新速率

我有一张叫做股票和货币的桌子

我还有名为[货币][即期汇率][卖出汇率][买入汇率]的专栏

这是一个PHP代码,我正试图改变我的解决方案

class CurrencyConverter {


       var $xml_file = "www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
       var $localhost, $root, $root, $mohamed, $stock_currency;
       var $exchange_rates = array();

       //Load Currency Rates

       function CurrencyConverter($host,$user,$pass,$db,$tb) {

          $this->localhost = $host;
          $this->root = $user;
          $this->root = $pass;
          $this->mohamed = $db;
          $this->stock_currency = $tb;

          $this->checkLastUpdated();

          $conn = mysql_connect($this->localhost,$this->root,$this->root);

          $rs = mysql_select_db($this->mohamed,$conn);

          $sql = "SELECT * FROM ".$this->stock_currency;

          $rs =  mysql_query($sql,$conn);

          while($row = mysql_fetch_array($rs)) {

             $this->exchange_rates[$row['currency']] = $row['rate'];
          }

       }

       /* Perform the actual conversion, defaults to £1.00 GBP to USD */
       function convert($amount=1,$from="GBP",$to="USD",$decimals=2) {

          return(number_format(($amount/$this->exchange_rates[$from])*$this->exchange_rates[$to],$decimals));
       }

       /* Check to see how long since the data was last updated */
       function checkLastUpdated() {
          $conn = mysql_connect($this->localhost,$this->root,$this->root);

          $rs = mysql_select_db($this->mohamed,$conn);

          $sql = "SHOW TABLE STATUS FROM ".$this->mohamed." LIKE '".$this->stock_currency."'";

          $rs =  mysql_query($sql,$conn);

          if(mysql_num_rows($rs) == 0 ) {

             $this->createTable();
          } else {
             $row = mysql_fetch_array($rs);
             if(time() > (strtotime($row["Update_time"])+(12*60*60)) ) {

                $this->downloadExchangeRates();
             }
          }
       }

       /* Download xml file, extract exchange rates and store values in database */

       function downloadExchangeRates() {
          $currency_domain = substr($this->xml_file,0,strpos($this->xml_file,"/"));
          $currency_file = substr($this->xml_file,strpos($this->xml_file,"/"));
          $fp = @fsockopen($currency_domain, 80, $errno, $errstr, 10);
          if($fp) {

             $out = "GET ".$currency_file." HTTP/1.1\r\n";
             $out .= "Host: ".$currency_domain."\r\n";
             $out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5\r\n";
             $out .= "Connection: Close\r\n\r\n";
             fwrite($fp, $out);
             while (!feof($fp)) {

                $buffer .= fgets($fp, 128);
             }
             fclose($fp);

             $pattern = "{<Cube\s*currency='(\w*)'\s*rate='([\d\.]*)'/>}is";
             preg_match_all($pattern,$buffer,$xml_rates);
             array_shift($xml_rates);

             for($i=0;$i<count($xml_rates[0]);$i++) {

                $exchange_rate[$xml_rates[0][$i]] = $xml_rates[1][$i];
             }

             $conn = mysql_connect($this->localhost,$this->root,$this->root);

             $rs = mysql_select_db($this->mohamed,$conn);

             foreach($exchange_rate as $currency=>$rate) {

                if((is_numeric($rate)) && ($rate != 0)) {

                   $sql = "SELECT * FROM ".$this->stock_currency." WHERE currency='".$currency."'";
                   $rs =  mysql_query($sql,$conn) or die(mysql_error());
                   if(mysql_num_rows($rs) > 0) {

                      $sql = "UPDATE ".$this->stock_currency." SET rate=".$rate." WHERE currency='".$currency."'";
                   } else {

                      $sql = "INSERT INTO ".$this->stock_currency." VALUES('".$currency."',".$rate.")";
                   }

                   $rs =  mysql_query($sql,$conn) or die(mysql_error());
                }

             }
          }
       }

       /* Create the currency exchange table */
       function createTable() {

          $conn = mysql_connect($this->localhost,$this->root,$this->root);

          $rs = mysql_select_db($this->mohamed,$conn);

          $sql = "CREATE TABLE ".$this->stock_currency." ( currency char(3) NOT NULL default '', rate float NOT NULL default '0', PRIMARY KEY(currency) ) ENGINE=MyISAM";

          $rs =  mysql_query($sql,$conn) or die(mysql_error());

          $sql = "INSERT INTO ".$this->stock_currency." VALUES('EUR',1)";

          $rs =  mysql_query($sql,$conn) or die(mysql_error());

          $this->downloadExchangeRates();
       }

    }

    ?> 

错误消息非常简单:
include('CurrencyConverter')失败,因为
C:\wamp\www\
目录中没有名为
CurrencyConverter
的文件。你是说
CurrencyConverter.php

第二条错误消息还抱怨
CurrencyConverter
不存在。这次它还在PHP的扩展目录中查找文件,但在那里找不到

第三个错误是抱怨
$x=new CurrencyConverter(…)
,因为您从未加载
CurrencyConverter
类-我假设该类位于缺少的文件中

这三条错误消息将通过包含该文件进行修正。包含
CurrencyConverter
类的PHP文件在哪里?您需要在
include
语句中输入文件名,而不是类名


注意-请不要使用
mysql.*
函数它们已弃用。使用或。

嘿,你的权利,我忘了在include中添加.php,但出现了另一个错误,即>致命错误:无法重新声明CurrencyConverter::$root在C:\wamp\www\CurrencyConverter.php的第5行,我在CurrencyConverter.php上也收到了错误消息,上面写着致命错误:无法重新声明CurrencyConverter::$root inC:\wamp\www\CurrencyConverter.php联机5@sirnur:您是否正在阅读错误消息?CurrencyConverter.php的第5行-
var$localhost、$root、$root、
-您要调用
$root
两次。但我们不是来为你工作的。请先通过阅读和调查错误代码来调试您的代码,如果您遇到问题,请再问另一个问题。我知道这听起来很正常,但我的密码和用户名是root,这不是家庭作业
<?php
   include('CurrencyConverter');
   $x = new CurrencyConverter('localhost','root','root','mohamed','Stock_Currency');
   echo $x->convert(1.00,'GBP','USD');
?>
( ! ) Warning: include(CurrencyConverter) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\wamp\www\Currency.php on line 2
Call Stack
#   Time    Memory  Function    Location
1   0.0005  672752  {main}( )   ..\Currency.php:0
( ! ) Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'CurrencyConverter' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\Currency.php on line 2
Call Stack
#   Time    Memory  Function    Location
1   0.0005  672752  {main}( )   ..\Currency.php:0
( ! ) Fatal error: Class 'CurrencyConverter' not found in C:\wamp\www\Currency.php on line 3
Call Stack
#   Time    Memory  Function    Location
1   0.0005  672752  {main}( )   ..\Currency.php:0