undefined variable

0 Kumi Festus · February 20, 2015
Guys I need help here. I don't  have the eyes to see this. I connected to a database like below:

$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "db_name";

$db = new mysqli($db_host,$db_username,$db_password,$db_name);

And I referred to it in a query on a different page like below:
$query = $db->query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
return (mysqli_result($query,0)==1)? true:false;

I get this error: undefined variable db,
What am I doing wrong here?

Post a Reply


- page 1
Oldest  Newest  Rating
+1 Mohamad Mirohamadi · February 24, 2015
you should get database link and pass it into mysqli_query as first paramter

the best approach here is too create a mysql class and put all this function in it
+1 Mohamad Mirohamadi · February 28, 2015
sorry for being late ,  I wish the following codes helps you:

 @$db = mysqli_connect($db_host,$db_username,$db_password,$db_name);
        die('Unable to connect to database [' . $db->connect_error . ']');

    function mysqli_result($resultObj, $columnIndex, $default = null)
        if(mysqli_num_rows($resultObj) == 0) return $default;
        //Checking if the column index is not numerical to call fetch_assoc so you can get column data by its name
        $row = is_numeric($columnIndex) ? mysqli_fetch_array($resultObj) : mysqli_fetch_assoc($resultObj);
        return $row[$columnIndex];

    function activate($email, $email_code)
        $email = trim($email);
        $email_code = trim($email_code);
        $checkQuery = "SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email' AND `email_code` = '$email_code' AND `active` = 0";
        $updateQuery = "UPDATE `users` SET `active` = 1 WHERE `email` = '$email'";
        $emailExists = mysqli_result(mysqli_query($db,$checkQuery) , 0);

            mysqli_query($db, $updateQuery);
            return true;
        return false;

function user_count(){
    $db = $GLOBALS['db'];
    $query = "SELECT COUNT(`user_id`) FROM `users` WHERE `active` = 1";
    return mysqli_result(mysqli_query($db,$query),0,0);
0 Mitch Mullvain · February 24, 2015
There is no mysqli_result

You could use a defined function like so:

function mysqli_result($res, $row, $field=0) { 
    $datarow = $res->fetch_array(); 
    return $datarow[$field]; 

0 Kumi Festus · February 24, 2015
I created the entire program using the old mysql and now I want to use the mysqli.
I already have a function like below:

function activate($email,$email_code){
$email = trim($email);
$email_code = trim($email_code);
if(mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email' AND `email_code` = '$email_code' AND `active` = 0"),0)== 1){
mysql_query($sql,"UPDATE `users` SET `active` = 1 WHERE `email` = '$email'");
return true;
return false;

AND another function like:

function user_count(){
return mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `active` = 1"),0);

How do I change these functions to accept mysqli? This is my problem now?
0 Mohamad Mirohamadi · February 24, 2015
for mysql you can use:


and for mysqli you can use:

0 Kumi Festus · March 1, 2015
I will implement it and see what happens
0 Mitch Mullvain · February 23, 2015
Did you include the database page. I always do that.

BTW, by include I mean the following:

<?php include ("database.php"); ?>


Also declaring new is used on classes, so unless you have a mysqli class in your program, declare
$db = mysqli_connect($host, $user, $pass, $dbname);
0 Kumi Festus · February 23, 2015
I have a folder containing all the functions,session,connection
includes/connect.inc.php //connects to database
includes/users.php //all functions
includes/general.php //functions to sanitize the functions
includes/init.php //contains sessions and all others

so the init.php has the following

include 'users.php';
include 'connect.inc.php';
include 'general.php';

and on the other pages that need to interact with the database with have the init.php, eg


Someone told me that I need a global varaible, like so;

$db = new mysqli("localhost","root","","db_name")

On the functions page I should declare something like below:

function email_exists($email){
global $db; //the global variable
$email = $_POST["email"];
$query = $db->query("SELECT COUNT(user_id) FROM tb WHERE email = '$email'");
return ($query->num_rows()===1) ? true:false;

I used the OOP method
0 Mitch Mullvain · February 23, 2015
That global variable is used when you have a class like this:
class mysqli {
   public function connect(){
       // Connect to database here

Then you would declare $db = new mysqli

Else you would use what I showed in the first post with mysqli_connect

That global $db you have is being equal to a new class that doesn't exist.

Another Edit
Just noticed your function and what you were talking about. That function needs to be in the mysqli class for the global variable to work.
0 Kumi Festus · February 23, 2015
Thanks a lot. I will use that
  • 1
  • 2



Server-side, HTML embedded scripting language used to create dynamic Web pages.