mysqli connection not working inside function? [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

This question already has an answer here:

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 Accessed:

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

serverList/api/rest.php

serverList/api/inc/restFunctions.php

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

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.

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

回答1:

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; 

See more here: http://php.net/manual/en/language.variables.scope.php



回答2:

In my opinion using global variables is not a good solution. You might override $link ($link is rather usual name for a variable you may be using for another purposes) variable in some scope by accident, resulting in lot's of confusion and difficult debugging. Just pass it as a function parameter - much cleaner and easier to read:

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

To be honest, even better solution would be using some generic classes/functions to establish your mysql connection like so:

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 } 


回答3:

Use global variable

function allServers(){  global $link  ... ... ... ... your code follows 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!