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 · 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 · 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;
  • 1
  • 2

PHP

126,397 followers
About

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

Links
Moderators