简体   繁体   English

使用PHP查询从SQL数据库为Sagepay返回值

[英]Return value from SQL database for Sagepay using PHP query

The website I'm working on is sending the incorrect Billing Country information to Sagepay (this field is now mandetory on 3.00 update) and I'm trying to find a way to pass the correct information within the PHP file. 我正在工作的网站正在向Sagepay发送不正确的“帐单国家/地区”信息(此字段现在是3.00更新的强制要求),而我正在尝试寻找一种在PHP文件中传递正确信息的方法。

The current line to send this information is: 发送此信息的当前行是:

$crypt .= "&BillingCountry=".$orderArray["country"];

The problem with this is that the "country" field in the orderArray gives the country name as set in our CMS, not the ISO code that Sagepay requires. 这样做的问题是orderArray中的“国家/地区”字段提供了我们CMS中设置的国家/地区名称,而不是Sagepay要求的ISO代码。

The correct ISO codes are held within an SQL database so the information is available. 正确的ISO代码保存在SQL数据库中,因此该信息可用。 The database table holds unique ID, name, ISO code, ISO number plus some other site-relavent information. 数据库表包含唯一的ID,名称,ISO代码,ISO编号以及一些其他有关站点的信息。

I need to replace the above crypt with this: 我需要用以下替换上面的地穴:

$crypt .="&BillingCountry=".$countryISO;

Where the $countryISO is added into the code as a query. $ countryISO作为查询添加到代码中的位置。 This query should look in the ISO Code column (isocode) of the Countries database table and return the ISO code value in correspondance with the "country" name in the orderArray. 此查询应在“国家/地区”数据库表的“ ISO代码”列(isocode)中查找,并返回与orderArray中“国家/地区”名称相对应的ISO代码值。

I've tried to put something together myself following php used in other files for the site which appear to be acheiving something similar. 我试着将自己放在网站其他文件中使用的php之后的东西放在一起,这些东西似乎正在实现类似的目的。 for example: 例如:

$countryISO = $dbA->query("SELECT isocode FROM $tableCountries WHERE name=".$orderArray["country"]);

I've also tried: 我也尝试过:

$countryISO = $dbA->retrieveAllRecordsFromQuery("SELECT isocode FROM $tableCountries WHERE name=".$orderArray["country"]);

There is a php file included on the site server that has the 'query' and 'retrieveAllRecordsFromQuery' functions, among others (eg count, fetch, retrieveAllRecords) and also includes a funciton relating to $dbA which I believe connects to the database. 站点服务器上包含一个php文件,该文件具有“查询”和“ retrieveAllRecordsFromQuery”功能,以及其他功能(例如,count,fetch,retrieveAllRecords),并且还包含一个与$ dbA有关的功能,我认为该功能连接到数据库。

I need some advice as to how I can achieve pulling the ISO Code from the SQL database. 我需要一些建议,以了解如何从SQL数据库中提取ISO代码。 Is my code anywhere near what I need? 我的代码是否在我需要的地方附近?

first, in sql statements, when you want to filter a string field, you should wrap the field value with single quote : ' or double quote: " . 首先,在sql语句中,当您要过滤字符串字段时,应使用single quote : 'double quote: "来包装字段值。

second, the code: 二,代码:

 $countryISO = $dbA->query("SELECT isocode FROM $tableCountries WHERE name=".$orderArray["country"]);

will not save the iso code in $countryISO . 不会将iso code保存在$countryISO to do that you must use fetch_row in mysqli : 为此,您必须在mysqli使用fetch_row

 $result=$mysqli->query("SELECT isocode FROM $tableCountries WHERE name='".$orderArray["country"]."'");
 $row=$result->fetch_row();
 $countryISO =$row[0];

Now you can change your previous code to: 现在,您可以将以前的代码更改为:

 $crypt .= " BillingCountry='".$countryISO."'";

PS As you did not mention, I assumed that $dbA is a mysqli object. PS正如您没有提到的,我假定$ dbA是mysqli对象。 if it is not, there is a similar procedure for PDO objects with small changes. 如果不是这样,则对PDO对象进行类似的更改很小的过程。

To create $mysqli object used in the above: 创建上面使用的$ mysqli对象:

 $mysqli=new mysqli($host, $user, $pass, $db);

I've managed to find a solution by using similar code used for another gateway for the website. 我设法通过使用用于网站另一个网关的类似代码来找到解决方案。 This has solved the Billing Country issue and is now sending data successfully to Sagepay on the 3.00 protocol. 这已解决了“帐单国家/地区”问题,并且现在已通过3.00协议成功将数据发送到Sagepay。

Solution: 解:

    $countryMatch = $orderArray["country"];
    $cResult = $dbA->query("select * from $tableCountries where name=\"$countryMatch\"");
    if (count($cResult) > 0) {
        $cRecord = $dbA->fetch($cResult);
        $customerCountry = $cRecord["isocode"];
    } else {
        $customerCountry = "";
    }

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

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