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

从另一个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);
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题