本文实例讲述了PHP实现货币换算的方法。分享给大家供大家参考。

具体实现代码如下:

/*

* File: CurrencyConverter.php

* Author: Simon Jarvis

* Copyright: 2005 Simon Jarvis

* Date: 10/12/05

* Link: http://www.white-hat-web-design.co.uk/articles/php-currency-conversion.php

*

* This program is free software; you can redistribute it and/or

* modify it under the terms of the GNU General Public License

* as published by the Free Software Foundation; either version 2

* of the License, or (at your option) any later version.

*

* This program is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* GNU General Public License for more details:

* http://www.gnu.org/licenses/gpl.html

*

*/

class CurrencyConverter {

var $xml_file = "www.ecb.int/stats/eurofxref/eurofxref-daily.xml";

var $mysql_host, $mysql_user, $mysql_pass, $mysql_db, $mysql_table;

var $exchange_rates = array();

//Load Currency Rates

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

$this->mysql_host = $host;

$this->mysql_user = $user;

$this->mysql_pass = $pass;

$this->mysql_db = $db;

$this->mysql_table = $tb;

$this->checkLastUpdated();

$conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);

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

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

$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->mysql_host,$this->mysql_user,$this->mysql_pass);

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

$sql = "SHOW TABLE STATUS FROM ".$this->mysql_db." LIKE '".$this->mysql_table."'";

$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.1rn";

$out .= "Host: ".$currency_domain."rn";

$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5rn";

$out .= "Connection: Closernrn";

fwrite($fp, $out);

while (!feof($fp)) {

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

}

fclose($fp);

$pattern = "{}is";

preg_match_all($pattern,$buffer,$xml_rates);

array_shift($xml_rates);

for($i=0;$i

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

}

$conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);

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

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

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

$sql = "SELECT * FROM ".$this->mysql_table." WHERE currency='".$currency."'";

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

if(mysql_num_rows($rs) > 0) {

$sql = "UPDATE ".$this->mysql_table." SET rate=".$rate." WHERE currency='".$currency."'";

} else {

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

}

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

}

}

}

}

/* Create the currency exchange table */

function createTable() {

$conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);

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

$sql = "CREATE TABLE ".$this->mysql_table." ( 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->mysql_table." VALUES('EUR',1)";

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

$this->downloadExchangeRates();

}

}

?>

上面的代码复制到一个新文件并将其保存为CurrencyConverter.php。当你需要转换包含类文件,称为“转换”功能。你需要输入自己的mysql数据库变量如登录详细信息。下面的例子将£2.50英镑转换成美元(美元)。

include('CurrencyConverter.php');

$x = new CurrencyConverter('your_host','your_username','your_password','your_database_name','your_table_name');

echo $x->convert(2.50,'GBP','USD');

?>

希望本文所述对大家的php程序设计有所帮助。

php mysql 金钱_PHP实现货币换算的方法相关推荐

  1. PHP折算,PHP实现货币换算的方法_PHP

    本文实例讲述了PHP实现货币换算的方法.分享给大家供大家参考. 具体实现代码如下: 代码如下: /* * File: CurrencyConverter.php * Author: Simon Jar ...

  2. mysql金钱字段_mysql货币使用什么字段类型?

    在mysql中,货币常使用Decimal和Numric类型的字段表示,这两种类型被MySQL实现为同样的类型:它们被用于保存值,该值的准确精度是极其重要的值,例如与金钱有关的数据. 开发中,货币在数据 ...

  3. php mysql 时间差_PHP中计算时间差的方法

    PHP中计算时间差的方法 PHP中计算时间差的几种方法,在php中计算时间差有时候是件麻烦的事!不过只要你掌握了日期时间函数的用法那这些也就变的简单了,以下是小编为大家搜索整理的PHP中计算时间差的方 ...

  4. php 管理 mysql 数据库 代码_安装并使用phpMyAdmin管理MySQL数据库_php

    学会使用基于web数据库的管理工具phpMyAdmin. 如果使用合适的工具,mysql数据库的管理就会为得相当简单.应用MySQL命令行方式需要对MySQL知识非常熟悉,对SQL语言也是同样的道理. ...

  5. php模糊搜索慢怎么办,MySQL中文模糊检索问题的解决方法_php

    mysql中文模糊检索问题的解决方法 来源:林兴陆 在 MySQL 下,在进行中文模糊检索时,经常会返回一些与之不相关的 记录,如查找 "-0x1.ebea4bfbffaacp-4%&quo ...

  6. php用到的mysql语句_PHP中常用到的一些MySQL语句_php

    在php开发中,经常会使用到mysql语句,下面就为您列举了一些经常使用的MySQL语句,希望对您平时的学习和开发工作能起到些许的作用. MySQL语句显示数据库或表: show databases; ...

  7. php备份mysql页面_如何用PHP的页面备份、恢复Mysql数据库_php

    //备份数据 $i   =   0; $crlf="/r/n"; $dbname="xgtqr"; global     $dbconn; http://www ...

  8. php查询mysql放到数组中_php查询mysql数据库并将结果保存到数组的方法

    本文实例讲述了php查询mysql数据库并将结果保存到数组的方法..具体分析如下: 这里主要用到了mysql_fetch_assoc函数 mysql_fetch_assoc语法如下: array my ...

  9. php员工删除,php+mysql删除指定编号员工信息的方法_PHP

    本文实例讲述了php+mysql删除指定编号员工信息的方法.分享给大家供大家参考.具体分析如下: 利用php提交员工编号给mysql,然后由mysql把数据接受并利用delete进行数据删除操作了,这 ...

最新文章

  1. 【Python】ubuntu14安装pycaffe环境:python2.7及依赖库
  2. 新手科普 | 探索机器学习模型,保障账户安全
  3. java字符串的替换replace、replaceAll、replaceFirst的区别
  4. ajax跨域请求问题总结
  5. 4.11 一维到三维推广-深度学习第四课《卷积神经网络》-Stanford吴恩达教授
  6. 7月的夏日北京必联网公司一行(图文)
  7. linux操作系统字段含义,Linux系统 /etc/fstab各个字段含义解释
  8. struts2 url传参中文乱码
  9. 《JavaScript精粹(修订版)》——1.6 使用括号和分号结束符(一致的编码方式)...
  10. 阿里云盘今日公测:无论用户是否付费,未来都不会限速
  11. mysql server8 jdbc_mysql8.0 jdbc连接注意事项
  12. get和post方式提交中文乱码问题解决方案
  13. 关于wordpress站点地图代码调试
  14. PAIP.ASP重复INCLUDE包含引起的重定义错误解决方案
  15. Windows Server 2019 配置DHCP
  16. 海思hisi v350报错librecovery (native:platform) should not link to libhi_common (native:vendor)
  17. 跨站漏洞解析-小韩网站编程安全系列一
  18. 程序复杂性度量方法-McCabe
  19. 搜狗校招编程题-建房子
  20. 渗透测试 ( 4 ) --- Meterpreter 命令详解

热门文章

  1. 学日语小技巧 让Office Word效劳
  2. 图片采集-输入关键词批量收集图片免费
  3. 程序员到底有没有必要买一台阿里云服务器?
  4. Turbo Autoencoder: Deep learning based channel code for point-to-point communication channels
  5. 相亲角、地摊,暗访小县城的夜市
  6. MSDN 、 RTM 、 OEM 、 VOL 四大版本之区别
  7. 你所不知的米聊和微信的前身——酷啦啦
  8. JS生成随机颜色(rgb)
  9. 诸葛——如何摆脱APP速死症?
  10. steamvr2.0 vr模式下,多相机共存