Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate table with correct currency rate
    primarykey
    data
    text
    <p>I am trying to get the exchange of 1 Pound to other currencies, at the moment I am getting Eur 1 to other currencies. for example 1 Eur = 0.81400 GBP, I can reverse this and get 1 GBP to Eur by dividing with each giving me exchange rate of 1.228501228501229.</p> <p>I want this for all the currencies, currently I am using php script to fetch the exchange rate. how can I alter my Stock_currency table and divide GBP to all the currencies and store them back in to Stock_currency table</p> <p>here is my PHPscript </p> <pre><code>&lt;?php class CurrencyConverter { var $xml_file = "www.ecb.int/stats/eurofxref/eurofxref-daily.xml"; var $localhost, $sirnur, $abc123, $mohamed, $stock_currency; var $exchange_rates = array(); //Load Currency Rates function CurrencyConverter($host,$user,$pass,$db,$tb) { $this-&gt;localhost = $host; $this-&gt;sirnur = $user; $this-&gt;abc123 = $pass; $this-&gt;mohamed = $db; $this-&gt;stock_currency = $tb; $this-&gt;checkLastUpdated(); $conn = mysql_connect($this-&gt;localhost,$this-&gt;sirnur,$this-&gt;abc123); $rs = mysql_select_db($this-&gt;mohamed,$conn); $sql = "SELECT * FROM ".$this-&gt;stock_currency; $rs = mysql_query($sql,$conn); while($row = mysql_fetch_array($rs)) { $this-&gt;exchange_rates[$row['currency']] = $row['rate']; } } /* Perform the actual conversion, defaults to £1.00 GBP to USD */ function convert($amount=1,$from="USD",$to="GBP",$decimals=2) { return(number_format(($amount/$this-&gt;exchange_rates[$from])*$this-&gt;exchange_rates[$to],$decimals)); } /* Check to see how long since the data was last updated */ function checkLastUpdated() { $conn = mysql_connect($this-&gt;localhost,$this-&gt;sirnur,$this-&gt;abc123); $rs = mysql_select_db($this-&gt;mohamed,$conn); $sql = "SHOW TABLE STATUS FROM ".$this-&gt;mohamed." LIKE '".$this-&gt;stock_currency."'"; $rs = mysql_query($sql,$conn); if(mysql_num_rows($rs) == 0 ) { $this-&gt;createTable(); } else { $row = mysql_fetch_array($rs); if(time() &gt; (strtotime($row["Update_time"])+(12*60*60)) ) { $this-&gt;downloadExchangeRates(); } } } /* Download xml file, extract exchange rates and store values in database */ function downloadExchangeRates() { $currency_domain = substr($this-&gt;xml_file,0,strpos($this-&gt;xml_file,"/")); $currency_file = substr($this-&gt;xml_file,strpos($this-&gt;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 = "{&lt;Cube\s*currency='(\w*)'\s*rate='([\d\.]*)'/&gt;}is"; preg_match_all($pattern,$buffer,$xml_rates); array_shift($xml_rates); for($i=0;$i&lt;count($xml_rates[0]);$i++) { $exchange_rate[$xml_rates[0][$i]] = $xml_rates[1][$i]; } $conn = mysql_connect($this-&gt;localhost,$this-&gt;sirnur,$this-&gt;abc123); $rs = mysql_select_db($this-&gt;mohamed,$conn); foreach($exchange_rate as $currency=&gt;$rate) { if((is_numeric($rate)) &amp;&amp; ($rate != 0)) { $sql = "SELECT * FROM ".$this-&gt;stock_currency." WHERE currency='".$currency."'"; $rs = mysql_query($sql,$conn) or die(mysql_error()); if(mysql_num_rows($rs) &gt; 0) { $sql = "UPDATE ".$this-&gt;stock_currency." SET rate=".$rate." WHERE currency='".$currency."'"; } else { $sql = "INSERT INTO ".$this-&gt;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-&gt;localhost,$this-&gt;sirnur,$this-&gt;abc123); $rs = mysql_select_db($this-&gt;mohamed,$conn); $sql = "CREATE TABLE ".$this-&gt;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-&gt;stock_currency." VALUES('EUR',1)"; $rs = mysql_query($sql,$conn) or die(mysql_error()); $this-&gt;downloadExchangeRates(); } } </code></pre> <p>the include file </p> <pre><code>&lt;?php include('CurrencyConverter.php'); $x = new CurrencyConverter('localhost','root','root','mohamed','Stock_Currency'); // mysql_query("ALTER TABLE Stock_currency add column We_sell varchar (20) ; ") or die(mysql_error()); // mysql_query("ALTER TABLE Stock_currency add column We_buy varchar (20) ; ") or die(mysql_error()); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload