PHP Undefined variable in functions and included scripts

后端 未结 3 1016
别跟我提以往
别跟我提以往 2021-01-22 19:34

I\'ve read a LOT of things about this problem, however I still cannot fix it.

In my functions file I declare a variable with a value like so:

$px_host =          


        
3条回答
  •  一生所求
    2021-01-22 20:27

    If you set the variable global, you will need to set it global in the function as well. In this case:

    $px_host = "localhost";
    
    function dbQuery($database, $reqquery){
        global $px_host;
        if(!$connect = mysql_connect($px_host, $px_dbuser, $px_dbpass)){
            exit("Error - cannot connect to MySQL server - " . mysql_error());
        }
    
        if(!$database = mysql_select_db($database)){
            exit("Error - cannot select database - " . mysql_error());
        }
    
        if(!$query = mysql_query($reqquery)){
            exit("Error - query error.");
        }
    
        return $query;
    }
    

    This should fix this problem.

提交回复
热议问题