未定义连接

时间:2018-06-04 07:25:26

标签: php pdo phpmyadmin

我有以下代码

    <?php
$host = "localhost";
$dbname = "hawkI";
$user = "root";
$password = "";

$userExist = false;
$userIP = null;
$userHasFinish = null;
$userLastPage = null;

try {
    $dbh = new PDO('mysql:host='.$host.';dbname='.$dbname, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

function getIPforBDD(){
    return $_SERVER['REMOTE_ADDR'];
}

function UpdateUserProfile()
{
    $requete = "SELECT * FROM users WHERE ip = ".getIPforBDD();
    $result = $dbh->query($requete);

    if($resultat->rowCount() == 0)
        exit();

    foreach($result as $ligne)
    {       
        $userIP = $ligne['ip'];
        $userhasFinish = $ligne['finish'];
        $userLastPage = $ligne['lastPage'];
    }
}

function CheckUserPosition()
{
    UpdateUserProfile();
    if(!$userExist)
        AddUser();

    return GetUserStatus();
}

function GetUserStatus()
{
    $page;
    if($userHasFinish)
        $page = "end.php";
    else
        $page = $userLastPage;

    return $page;
}

function AddUser()
{
    $requete = "INSERT INTO users (ip, finish, lastPage) VALUES (".getIPforBDD().", ".false.", questionnaire_initial.php)";
    $result = $dbh->query($requete);
}

function SavePageInBDD($page){
    $requete = "UPDATE users SET lastPage = '.$page.' WHERE ip = ".getIPforBDD();
    $result = $dbh->query($requete);
} 
?>

但是,我使用它时遇到问题

  

(!)注意:未定义的变量:C:\ wamp64 \ www \ HawkI \ bdd.php中的dbh   在第66行

我不能正确理解PHP是如何使用它的第一次使用它,但我试图制作

global $dbh = new PDO('mysql:host='.$host.';dbname='.$dbname, $user, $password);

这也不起作用。

此外,似乎放在函数之外的值不像js那样是全局的,我怎样才能从任何地方访问某些东西(比如包含该文件的文件)

谢谢

3 个答案:

答案 0 :(得分:2)

更好的方法是做这样的事情:

function getDB(){
  $dbh = null;
  try {
    $dbh = new PDO('mysql:host='.$host.';dbname='.$dbname, $user, $password);
   } catch (PDOException $e) {
      echo 'Connection failed: ' . $e->getMessage();
   }
   return $dbh;
}

而且在你的职能部门中这样做:

function AddUser()
{
    $dbh = getDB();
    if(!is_null($dbh)){
         $requete = "INSERT INTO users (ip, finish, lastPage) VALUES (".getIPforBDD().", ".false.", questionnaire_initial.php)";
         $result = $dbh->query($requete);
    }
}

答案 1 :(得分:0)

要在函数中使用$dbh,您需要在函数范围内包含global关键字。

您可以在http://php.net/manual/en/language.variables.scope.php#language.variables.scope.global

找到全局关键字说明
function AddUser()
{
    global $dbh;
    $requete = "INSERT INTO users (ip, finish, lastPage) VALUES (".getIPforBDD().", ".false.", questionnaire_initial.php)";
    $result = $dbh->query($requete);
}

答案 2 :(得分:0)

您可以像这样使用

$host = "localhost";
$dbname = "hawkI";
$user = "root";
$password = "";
$userExist = false;
$userIP = null;
$userHasFinish = null;
$userLastPage = null;
$dbh = NULL;
function db () {
    try {   
        if ($GLOBALS['dbh']===NULL){ 
            $GLOBALS['dbh'] = new PDO('mysql:host='.$host.';dbname='.$dbname, $user, $password);
        }
        return $GLOBALS['dbh'];    
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
}

function SavePageInBDD($page){
    $dbh = db();       
    $requete = "UPDATE users SET lastPage = '.$page.' WHERE ip = ".getIPforBDD();
    $result = $dbh->query($requete);
}