mysqli连接无法在函数内部工作?

时间:2013-07-09 17:44:37

标签: php mysql function mysqli

我在php函数中执行mysql查询时遇到一些问题。我得到的错误是

Notice: Undefined variable: link in C:\path\api\inc\restFunctions.php on line 16

有几个文件互相调用,所以我将尝试概述必要的信息。

访问过的网址:

localhost/serverList/api/rest.php?action=allServers

SERVERLIST / API / rest.php

<?php
include 'inc/restFunctions.php';

$possibleCalls = array('allServers','allEnvs','allTypes','false');
if(isset($_GET['action'])){
    $action = $_GET['action'];
}
else{
    $action = 'false';
}


if(in_array($action,$possibleCalls)){
    switch ($action){
        case 'allServers':
            $return = allServers();
        break;
        case 'allEnvs':
            $return = allEnvs();
        break;
        case 'allTypes':
            $return = allTypes();
        break;
        case 'false':
            $return = falseReturn();
        break;
    }
}

SERVERLIST / API / INC / restFunctions.php

<?php
include ('inc/config.php');

function allServers(){
    $serverInfoQuery = "SELECT * FROM servers"
    $allServerResults = $link->query($serverInfoQuery);
    $json = array();
    while($row = $allServerResults->fetch_assoc()){
        $json[]['serverID'] = $row['serverID'];
        $json[]['environment'] = $row['environment'];
        $json[]['type'] = $row['type'];
        $json[]['serverIP'] = $row['serverIP'];
        $json[]['serverDescription'] = $row['serverDescription'];
        $json[]['serverCreatedBy'] = $row['serverCreatedBy'];
        $json[]['serverCreatedDtTm'] = $row['serverCreatedDtTm'];
        $json[]['serverUpdatedBy'] = $row['serverUpdatedBy'];
        $json[]['serverUpdatedDtTm'] = $row['serverUpdatedDtTm'];
    }
    $jsonResults = json_encode($json);
    return $jsonResults;
}
?>

SERVERLIST / API / INC / config.php中

<?php
$host = 'localhost';
$user = 'userName';
$password = 'password';
$database = 'database';
$link = new mysqli($host, $user, $password, $database);
if (mysqli_connect_errno()) {
    exit('Connect failed: '. mysqli_connect_error());
}
?>

我已经验证了被调用的查询是否有效。我还验证了连接信息(上面掩盖的)通过使用查询数据库的该软件的不同页面来工作。

我假设我一定错过了某个引用或某个地方,但我对它的位置感到困惑。

3 个答案:

答案 0 :(得分:9)

问题在于PHP变量范围。在第一次引用 $ link 变量之前,在 allServers()函数中添加此行:

global $link;

在此处查看更多信息: http://php.net/manual/en/language.variables.scope.php

答案 1 :(得分:1)

在我看来,使用全局变量不是一个好的解决方案。您可能会在某个范围内意外覆盖$ link($ link是您可能用于其他目的的变量的通常名称)变量,从而导致许多混乱和调试困难。 只需将其作为函数参数传递 - 更清晰,更易于阅读:

function AllServers($link) {
    $serverInfoQuery = "SELECT * FROM servers";
    $allServerResults = $link->query($serverInfoQuery);
    //More PHP code
}    

if(in_array($action,$possibleCalls)){
    switch ($action){
        case 'allServers':
            $return = allServers($link);
        break;
    }
}

老实说,更好的解决方案是使用一些通用的类/函数来建立你的mysql连接,如下所示:

class DB {

    private static $link = null;

    public static function getConnectionResource() {
        //In that way we "cache" our $link variable so that creating new connection 
        //for each function call won't be necessary
        if (self::$link === null) {
            //Define your connection parameter here
            self::$link = new mysqli($host, $user, $password, $database);
        }
        return self::$link;
    }
}

function getAllServers() {
    $link = DB::getConnectionResource();
    //Preform your query and return results
}

答案 2 :(得分:-1)

使用全局变量

function allServers(){

global $link

...
...
...
... your code follows