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?
Thanks

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Kumi Festus · April 5, 2015
Hi,
I am beginning to use the mysqli function.
I want to use OOP, Like so :
$connect = new mysqli("localhost","root","","dbname);

$query = $connect->query("SELECT a FROM table WHERE a = b");
$numRows = $query->num_row($query);

So how do I use OOP to create the mysqli function like below:
 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];
   }

Thanks a lot for your help
0 Kumi Festus · March 1, 2015
Thanks,
I will implement it and see what happens
+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);
    if($db->connect_errno>0)
    {
        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);

        if($emailExists)
        {
            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 Mohamad Mirohamadi · February 24, 2015
for mysql you can use:





 

and for mysqli you can use:


+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
0 Kumi Festus · February 24, 2015
OK
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;
}else{
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 Mitch Mullvain · February 24, 2015
There is no mysqli_result


You could use a defined function like so:


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

?>
0 Kumi Festus · February 23, 2015
I have one other bothering me, using mysql:
mysql_connect('localhost','root','');
mysql_select_db('db_name');

$query = mysql_query("SELECT COUNT(id') FROM table_name WHERE place = '$palce'");

return mysql_result($query,0) //my issue is here, the mysql_result();

Using mysqli:
$connect = mysqli_connect('localhost','root','','db_name');

$query = mysqli_query($connect,"SELECT COUNT(id') FROM table_name WHERE place = '$palce'");


Is it correct to say;

return mysqli_result($query,0)  

How do I transition from

return mysql_result($query,0) 

to

return mysqli_result($query,0) //here 

.Thanks for your help. I have searched a lot, but I don't get any proper answer;
0 Kumi Festus · February 23, 2015
Thanks a lot. I will use that
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.
  • 1
  • 2

PHP

107,079 followers
About

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

Links
Moderators