duan1443 2016-08-10 12:01
浏览 143
已采纳

从另一个php文件调用mysqli函数

I am importing transaction data and saving it into a database. Some of the transaction data has missing fields, so I am manually updating them using the following function from a separate file (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);
    }
}

To clean up my code a bit in file1.php, i am calling the file2 function on file 1.php using:

$conn = mysqli_connect($host, $user, $pass, $database);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
include 'categoryRules.php';
categoryRules();
mysqli_close($conn);

I am getting the following response when doing so:

Error: UPDATE Transactions SET cat1 = 'Shops', cat2 = 'Supermarkets and Groceries' WHERE name LIKE '%shipt%'

Error: UPDATE Transactions SET cat1 = 'Shops', cat2 = 'Supermarkets and Groceries' WHERE name LIKE '%shipt%'

Error: UPDATE Transactions SET cat1 = 'Shops', cat2 = 'Food and Beverage Store', cat3 = 'Beer, Wine and Spirits' WHERE name LIKE '%liquor%'

Error: UPDATE Transactions SET cat1 = 'Service', cat2 = 'Telecommunication Services' WHERE name LIKE '%vzwrls%'

Error: UPDATE Transactions SET cat1 = 'Recreation', cat2 = 'Gyms and Fitness Centers' WHERE name LIKE '%classpass%'

It seems like the functions may be running in file 2.php before they get to file1.php, which is where I open, access, and close the database link. Is there a way to import them into file 1? Is there a better way to clean up this portion of the code for file 1.php besides creating file 2.php?

  • 写回答

1条回答 默认 最新

  • dr637349 2016-08-10 12:27
    关注

    Ahh of course, I should have thought of this earlier

    You make a connection outside the function so $conn is not visible inside the function, just pass $conn to the funtion as a parameter and it will all magically work. Its a simple scope issue!

    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);
        }
    }
    

    Now the main script should be

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择