PHP个性化connect.php无效

时间:2016-07-24 14:26:29

标签: php

我在为我的网站制作新系统时遇到问题。我正在开发一个新的connect.php和approved.php(用于其他方式) 我似乎无法让它发挥作用。这是approved.php

approved.php

<?php
    include_once "connect.php";
    include_once "strings.php";
    connect($conn[0][0], $conn[1][2], $conn[2][2], $conn[3][0]);

    //echo $newConn;

    mysqli_query($newConn, "SELECT * FROM `loginsystem`") or die("Could not connect");
?>

这是 connect.php

<?php
    function connect($servername, $username, $password, $dbname)
    {
        include "strings.php"; 
        if ($servername == $conn[0][0] && $username == $conn[1][0] && $password == $conn[2][0] && $dbname == $conn[3][0] || 
        $servername == $conn[0][0] && $username == $conn[1][1] && $password == $conn[2][1] && $dbname == $conn[3][0] || 
        $servername == $conn[0][0] && $username == $conn[1][2] && $password == $conn[2][2] && $dbname == $conn[3][0])
        {
            $newConn;
            if ($newConn = mysqli_connect($conn[0][0], $conn[1][0], $conn[2][0], $conn[3][0])
            || $newConn = mysqli_connect($conn[0][0], $conn[1][1], $conn[2][1], $conn[3][0])
            || $newConn = mysqli_connect($conn[0][0], $conn[1][2], $conn[2][2], $conn[3][0]))
            {
                echo $processMsg;
            }
            else 
            {
                echo $errorMsg;
                die();
            }
            return $newConn;
        }
        else if ($servername == $conn[0][0] && $username == $conn[1][0] && $password == $conn[2][0] && $dbname == $conn[3][1] || 
        $servername == $conn[0][0] && $username == $conn[1][1] && $password == $conn[2][1] && $dbname == $conn[3][1] ||
        $servername == $conn[0][0] && $username == $conn[1][2] && $password == $conn[2][2] && $dbname == $conn[3][1])
        {
            if ($newConn = mysqli_connect($conn[0][0], $conn[1][0], $conn[2][0], $conn[3][1])
            || $newConn = mysqli_connect($conn[0][0], $conn[1][1], $conn[2][1], $conn[3][1])
            || $newConn = mysqli_connect($conn[0][0], $conn[1][2], $conn[2][2], $conn[3][1]))
            {
                echo $processMsg;
            }
            else 
            {
                echo $errorMsg;
                die();
            }
            return $newConn;
            }
        else 
        {
            die("Something went wrong with our systems");
        }
    }
?>

最后 strings.php 取出敏感信息

<?php
    //Information taken out for stack overflow question - not everything is here
    include_once("connect.php");
    $tablesChromeAds = ["food", "software", "hardware", "home", "outdoor", "indooract", "services", "other"];
    $conn = array(array($server) /*both our servers go under localhost*/, array($admin, $admin1, $localAdmin), array($adminPass, $admin1Pass, $localAdminPass), array($dbGeneralDB, $dbBrowserExtensions));
    $newConn;
    $errorMsg = "<script type='text/javascript'>Notification.requestPermission().then(function(result) {console.log(result);});var notification = new Notification('Smart Lead Advertisement Error', {icon: '$logo',body: 'Cannot connect to database'});</script>";
    $processMsg = "<script type='text/javascript'>Notification.requestPermission().then(function(e) {console.log(e);});var notification = new Notification('Smart Lead Advertisement', {icon: '$logo',body: 'Processing request...'});</script>";

    //Get the value for the presale license's from the mysql database to be stored within a variable which can be later accessed from any file 

    $num = 50;
?>

现在我知道的所有内容都包含正确。在我尝试连接之前没有错误发生。我没有这个 之前的问题。错误如下所示

Notice: Undefined variable: newConn in C:\xampp\htdocs\smartlead\php\approved.php on line 6

Notice: Undefined variable: newConn in C:\xampp\htdocs\smartlead\php\approved.php on line 8

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\smartlead\php\approved.php on line 8

所以我知道newConn变量未正确返回的一件事。但为什么?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

u_mulder

评论
$newConn =  connect($conn[0][0], $conn[1][2], $conn[2][2], $conn[3][0]);

    //echo $newConn;

    mysqli_query($newConn, "SELECT * FROM `loginsystem`") or die("Could not connect");

如果您的connection未成立,请尝试使用echo mysqli_connect_error();函数

connect.php
<?php

function connect($servername, $username, $password, $dbname) {
    include "strings.php";
    if ($servername == $conn[0][0] && $username == $conn[1][0] && $password == $conn[2][0] && $dbname == $conn[3][0] ||
            $servername == $conn[0][0] && $username == $conn[1][1] && $password == $conn[2][1] && $dbname == $conn[3][0] ||
            $servername == $conn[0][0] && $username == $conn[1][2] && $password == $conn[2][2] && $dbname == $conn[3][0]) {
        $newConn;
        if ($newConn = mysqli_connect($conn[0][0], $conn[1][0], $conn[2][0], $conn[3][0]) || $newConn = mysqli_connect($conn[0][0], $conn[1][1], $conn[2][1], $conn[3][0]) || $newConn = mysqli_connect($conn[0][0], $conn[1][2], $conn[2][2], $conn[3][0])) {
            echo $processMsg;
        } else {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            echo $errorMsg;
            die();
        }
        return $newConn;
    } else if ($servername == $conn[0][0] && $username == $conn[1][0] && $password == $conn[2][0] && $dbname == $conn[3][1] ||
            $servername == $conn[0][0] && $username == $conn[1][1] && $password == $conn[2][1] && $dbname == $conn[3][1] ||
            $servername == $conn[0][0] && $username == $conn[1][2] && $password == $conn[2][2] && $dbname == $conn[3][1]) {
        if ($newConn = mysqli_connect($conn[0][0], $conn[1][0], $conn[2][0], $conn[3][1]) || $newConn = mysqli_connect($conn[0][0], $conn[1][1], $conn[2][1], $conn[3][1]) || $newConn = mysqli_connect($conn[0][0], $conn[1][2], $conn[2][2], $conn[3][1])) {
            echo $processMsg;
        } else {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            echo $errorMsg;
            die();
        }
        return $newConn;
    } else {
        die("Something went wrong with our systems");
    }
}

?>

答案 1 :(得分:1)

以下是一个函数赋值如何在PHP中工作的示例。

function test() {
     $moose = 'Wow';
     return $moose;
}
test();
echo 'empty->' . $moose . "<-empty"; //undefined;
$moose = test();
echo $moose;

关于第一个函数调用test()的注意事项我们对返回的值没有任何作用,所以它只是丢失了。所有变量也与其功能相关。您可以在函数中调用变量$cattle,然后在脚本中调用$moose,并且所有$moose引用都可以。

演示:https://eval.in/611062