为什么我不能在PHP和MySQL中运行两次查询?

时间:2016-12-17 07:22:31

标签: php mysql

以下是我从PHP运行MySQL查询的代码:

mysqli_connect.php

<?php

    DEFINE ('DB_USER', [my_username]);
    DEFINE ('DB_PASSWORD', [my_password]);
    DEFINE ('DB_HOST', 'localhost');

    $databases = array();
    $db_school = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, 'db_school')
    OR die('Could not connect to MySQL.db_school');
    $databases['db_school'] = $db_school;

    $db_family_finance = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, 'db_family_finance')
    OR die('Could not connect to MySQL.db_family_finance');
    $databases['db_family_finance'] = $db_family_finance;

?>

get_mysql_data.php

<?php    

    function query_database($query) {
        require_once('../mysqli_connect.php');

        $response = mysqli_query($db_school, $query);

        if ($response) {
            echo "ran query!<br>";
        } else {
            echo "couldn't run query!<br>";
        }

        return $response;
    }

    query_database("SELECT * FROM tests"); //prints out "ran query!"
    query_database("SELECT * FROM tests"); //prints out "couldn't run query!"

?>

我正在运行两次相同的功能。它第一次返回我想要的东西。第二次没有。为什么要这样做?

我对上面的代码进行了一些更改,现在我将其打印到屏幕上:

ran query!

Notice: Undefined variable: db_school in C:\xampp\htdocs\websites\tutorials\get_mysql_data.php on line 6

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\websites\tutorials\get_mysql_data.php on line 6
couldn't run query!

0 个答案:

没有答案