-2

I am new to sqli ,just started learning oops . I have this code in my db.php

$db = new mysqli('localhost', 'root', '', 'mydb');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

Another file func.php

<?php
include_once 'db.php';
function get_users()
   {        

     $sql="SELECT * from users";    
     $result=$db->query($sql);    

     while($row = $result->fetch_assoc())
            {
                $data[]=$row;
            }

     return $data;
}

var_dump(get_users());

Error : Undefined variable: db

and when i use

global $db; inside function

Error : Call to a member function query() on a non-object in

What is the problem here and how can i correct it ?

Ace
  • 793
  • 8
  • 21
  • Try `return get_users($data);` – Funk Forty Niner Oct 30 '13 at 08:05
  • @Fred-ii- same non-object problem – Ace Oct 30 '13 at 08:15
  • Try putting what I wrote above, outside your last closing brace. @user2894116 – Funk Forty Niner Oct 30 '13 at 08:24
  • @Fred-ii- when i dont pass $db i get both the errors above & when i pass $db i get `Error: Call to undefined method mysqli::fetch_assoc()`. after using `return get_users($data);` – Ace Oct 30 '13 at 08:32
  • @Fred-ii- That's how the site works, grooming a list of canonical questions rather than a scatter poop of unrelated ones. – Ja͢ck Oct 30 '13 at 10:08
  • @Fred-ii- Your answer was probably downvoted by YCS. – Ja͢ck Oct 30 '13 at 10:14
  • @user2894116 [**Here's a Pastebin file I made**](http://pastebin.com/cpnZsMK3) they can't downvote that. I hope it serves you well. – Funk Forty Niner Oct 30 '13 at 12:07
  • 1
    `To People who added the question as DUPLICATE` I tried finding my answer in the link . If i had an answer or found something useful i woudnt have asked the question in first place. Don't know if the site provide REP for adding a duplicate tag. I think when asking question we should also put a list of links and write `Hey people i dint find anything useful here so my question is not duplicate.` Please dont downvote a question if someone wants to learn – Ace Oct 30 '13 at 12:09
  • @Fred-ii- tnx buddy.. i found the error. – Ace Oct 30 '13 at 12:19
  • @user2894116 You're welcome and glad to hear that. What was the error? I'm curious. – Funk Forty Niner Oct 30 '13 at 12:20
  • @Fred-ii- I think it was my localhost. Deleted the file , wrote the same code again and now its working :) – Ace Oct 30 '13 at 12:23
  • @user2894116 Right on, a happy ending after all, cheers. – Funk Forty Niner Oct 30 '13 at 12:25

1 Answers1

0

Instead of making it global, pass the $db as a function parameter and you can refer this answer for the error (Error : Call to a member function query() on a non-object in) you get after you are using global keyword.

Passing $db as a function parameter...

function example($db) {
   //Use the $db here
}

While you call the function, pass your $db variable like example($db);

Also you might like to read about variable scope

Community
  • 1
  • 1
Mr. Alien
  • 140,764
  • 31
  • 277
  • 265