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 2
Oldest  Newest  Rating
0 Kumi Festus · March 1, 2015
Thanks,
I will implement it and see what happens
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
  • 1
  • 2

PHP

107,110 followers
About

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

Links
Moderators