我正在导入交易数据并将其保存到数据库中。某些事务数据缺少字段,因此我使用以下函数从单独的文件(file2.php)手动更新它们:
function categoryRules() {
//rules for transactions
$sql = "UPDATE Transactions
SET cat1 = 'Shops', cat2 = 'Supermarkets and Groceries'
WHERE name LIKE '%shipt%'";
if (mysqli_query($conn, $sql)) {
echo "SHIPT UPDATE PERFORMED";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$sql = "UPDATE Transactions
SET cat1 = 'Shops', cat2 = 'Food and Beverage Store', cat3 = 'Beer, Wine and Spirits'
WHERE name LIKE '%liquor%'";
if (mysqli_query($conn, $sql)) {
echo "LIQUOR UPDATE PERFORMED";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$sql = "UPDATE Transactions
SET cat1 = 'Service', cat2 = 'Telecommunication Services'
WHERE name LIKE '%vzwrls%'";
if (mysqli_query($conn, $sql)) {
echo "VERIZON UPDATE PERFORMED";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$sql = "UPDATE Transactions
SET cat1 = 'Recreation', cat2 = 'Gyms and Fitness Centers'
WHERE name LIKE '%classpass%'";
if (mysqli_query($conn, $sql)) {
echo "VERIZON UPDATE PERFORMED";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
要在file1.php中清理我的代码,我在文件1.php上调用file2函数:
$conn = mysqli_connect($host, $user, $pass, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
include 'categoryRules.php';
categoryRules();
mysqli_close($conn);
这样做时我得到以下回复:
错误:UPDATE Transactions SET cat1 =&#39; Shops&#39;,cat2 =&#39;超市 和杂货&#39;名称LIKE&#39;%shipt%&#39;
错误:UPDATE Transactions SET cat1 =&#39; Shops&#39;,cat2 =&#39;超市和杂货&#39;名称LIKE&#39;%shipt%&#39;
错误:更新交易SET cat1 =&#39;商店&#39;,cat2 =&#39;食品和饮料商店&#39;,cat3 =&#39;啤酒,葡萄酒和烈酒&#39;名称喜欢&%39%%%&#39;
错误:更新交易SET cat1 =&#39;服务&#39;,cat2 =&#39;电信服务&#39;名称喜欢&#39;%vzwrls%&#39;
错误:UPDATE Transactions SET cat1 =&#39; Recreation&#39;,cat2 =&#39;健身房和健身中心&#39;名称LIKE&#39;%classpass%&#39;
似乎函数可能在文件2.php中运行,然后才到达file1.php,这是我打开,访问和关闭数据库链接的地方。有没有办法将它们导入文件1?除了创建文件2.php之外,还有更好的方法来清理文件1.php的这部分代码吗?
答案 0 :(得分:0)
啊,当然,我应该早点想到这个
您在函数外部建立了连接,因此$conn
在函数内部不可见,只需将$conn
作为参数传递给函数,它就会神奇地起作用。这是一个简单的范围问题!
categoryRules.php
function categoryRules($conn) { // <-- add parameter
//rules for transactions
$sql = "UPDATE Transactions
SET cat1 = 'Shops', cat2 = 'Supermarkets and Groceries'
WHERE name LIKE '%shipt%'";
if (mysqli_query($conn, $sql)) {
echo "SHIPT UPDATE PERFORMED";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$sql = "UPDATE Transactions
SET cat1 = 'Shops', cat2 = 'Food and Beverage Store', cat3 = 'Beer, Wine and Spirits'
WHERE name LIKE '%liquor%'";
if (mysqli_query($conn, $sql)) {
echo "LIQUOR UPDATE PERFORMED";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$sql = "UPDATE Transactions
SET cat1 = 'Service', cat2 = 'Telecommunication Services'
WHERE name LIKE '%vzwrls%'";
if (mysqli_query($conn, $sql)) {
echo "VERIZON UPDATE PERFORMED";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$sql = "UPDATE Transactions
SET cat1 = 'Recreation', cat2 = 'Gyms and Fitness Centers'
WHERE name LIKE '%classpass%'";
if (mysqli_query($conn, $sql)) {
echo "VERIZON UPDATE PERFORMED";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
现在主要脚本应该是
$conn = mysqli_connect($host, $user, $pass, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
include 'categoryRules.php';
// add $conn parameter to the call
categoryRules($conn);
mysqli_close($conn);