2013-08-04 27 views
-2

我收到提到的錯誤。我有conn.php中的數據庫連接,其中包括在fn.php中。然而,在一個特定的行它給錯誤注意:未定義變量:rezistent。凡rezistent指數據庫

注意:未定義的變量:rezistent在fn.php第16行

第16行是:

$a = mysqli_query($rezistent, "SELECT is_verified FROM users WHERE veri_key = '$key'") or die(mysqli_error()); 

這裏是我的conn.php

<?php 
    $user = "mmoin"; 
    $pass = "pass"; 
    $host = "localhost"; 
    $dbname = "rezistent"; 

    $rezistent = mysqli_connect($host, $user, $pass, $dbname) or die("cannot connect with database"); 
    ?> 

和這裏的fn.php

<?php 
    include "conn.php"; 

    function hashit($v){ 
     $hash = md5($v); 
     $hash .= rand(11,99); 
     return $hash; 
    } 

    function user_verification($k){ 
    $account_type = substr($k, 0, 1); 
    $key = substr($k, 1); 
    $msg_to_display = ""; 

    if($account_type == "h" || $account_type == "t"){ 
     $a = mysqli_query($rezistent, "SELECT is_verified FROM users WHERE veri_key = '$key'") or die(mysqli_error()); 

     $rows = mysqli_num_rows($a); 
     if($rows > 0){ 
      mysqli_query($rezistent, "UPDATE users SET is_verified = '1' WHERE veri_key='$key'"); 
      $msg_to_display = "User successfully verified. Please use the login link to login with your credentials."; 
     } 
    } 
    else{ 
     $msg_to_display = "There seems to be a problem with the verification key. Please try again from the link provided in the email."; 
    } 

    return $msg_to_display; 
    } 
    ?> 

可能是什麼問題?我曾嘗試在函數之前立即連接數據庫,但它仍然提供相同的通知消息。

+1

在你的函數中包含conn.php user_verification() – Horen

+1

'mysqli_query()'期望查詢是第一個參數,資源是第二個參數,所以你的參數是相反的。但我不認爲這是問題。你有沒有嘗試'echo' $ rezistent'來查看它是否返回資源句柄? –

+0

爲了闡明@ Horen的評論,值'$ rezistent'不在你的函數範圍內。你可能需要按照他的建議包含它,將它作爲參數傳遞給函數,或者將其作爲全局函數加入。 –

回答

0

這是因爲$rezistent沒有定義在函數user_verification的範圍內,這並不意味着數據庫連接被拒絕。

解決方案:

發送$rezistent作爲第二個參數的功能。

function user_verification($k, $rezistent){ 
1
  • 這是一個通知,而不是錯誤
  • 那個變量真的沒有定義(你可以依靠php,它不是如果php說的那麼...)。
  • 你在頂層聲明該變量,但是嘗試在函數中使用它。這是行不通的。您必須將該變量作爲全局變量進行訪問。目前它被解釋爲函數的本地變量,它不存在。甚至更好:將變量作爲參數傳遞給函數。