mysqli连接不工作的功能? [英] mysqli connection not working inside function?

查看:104
本文介绍了mysqli连接不工作的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php函数内部执行mysql查询时遇到了一些问题。我收到的错误是:

I'm having some problems performing a mysql query inside a php function. The error I am getting is

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

有几个文件互相调用,因此我会尝试概述必要的信息。

There are several files calling each other so I will attempt to outline the necessary information.

访问的URL:

URL Accessed:

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

serverList / api / rest.php

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 p>

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

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

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

I have verified that the query being called works. I also verified that the connection info (masked above) works by using a different page of this software that queries the db.

我假设我必须错过报价或paren某处,但我很困惑,它可能是在哪里。

I'm assuming I must have missed a quote or paren somewhere, but I'm baffled as to where it might be.

推荐答案

问题是与PHP变量范围。在您首次引用 $ link 变量之前,在 allServers()函数中添加以下行:

The problem is with PHP variable scoping. Add this line inside of allServers() function before you refer to the $link variable for the first time:

global $link;

更多此处:
http://php.net/manual/en/language.variables.scope.php

这篇关于mysqli连接不工作的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆