-3

I am getting the mentioned error. I have the database connection in conn.php which I am including in fn.php. However, on a particular line it is giving the error

Notice: Undefined variable: rezistent in fn.php on line 16

Line 16 is:

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

Here's my conn.php

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

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

and here's the 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;
    }
    ?>

What can be the problem? I have tried even connecting with database immediately before the function but it still gives the same notice message.

  • 1
    Include conn.php in your function user_verification() – Horen Aug 04 '13 at 07:18
  • 1
    `mysqli_query()` expects the query as the first parameter and the resource as the second, so your parameters are reversed. But I don't think that's the problem. Have you tried to `echo` $rezistent` to see if it returns a resource handle? – Nick Coons Aug 04 '13 at 07:19
  • To clarify on @Horen's comment, the value `$rezistent` isn't within the scope of your function. You'll either need to include it as he suggests, pass it as a parameter to the function, or pull it in as a global. – Nick Coons Aug 04 '13 at 07:20
  • 1
    okay, great. But isn't this include something global in php? – Muhammad Talha Moin Aug 04 '13 at 07:20

2 Answers2

1
  • that is a notice, not an error.
  • that variable really is not defined (you can rely on php that it is not if php says so...).
  • you declare that variable on top level, but try to use it inside a function. That won't work. You'd have to access the variable as a global variable. Currently it is interpreted as a variable local to the function, where it does not exist. Better even: hand over the variable as a parameter to the function.
arkascha
  • 41,620
  • 7
  • 58
  • 90
0

This is because $rezistent not defined inside the scope of the function user_verification, this is not mean that database connection refused.

Solution:

Send $rezistent as second parameter to the function.

function user_verification($k, $rezistent){