5

When I do my DB connection like this:

$conn = new MySQLi(RUBYDBUSER, RUBYDBNAME, RUBYDBPASS, RUBYDBDATA);
if($conn->errno) {
    throw new Exception($conn->connect_error, $conn->connect_errno);
}

and I want to run a prepared statement like this:

public function getSitename() {
            $stmt = $conn->prepare("SELECT value FROM cms_options WHERE title = 'sitename' ");
            $db->stmt_init();
            $stmt->execute();
            $stmt->bind_result($sitename);
            if($stmt->num_rows > 0) {
                while ($stmt->fetch) {
                    return $sitename;
                }
            }
        }

I get this error:

Notice: Undefined variable: conn in C:\xampp\htdocs\ruby\app\includes\classes\class.core.php on line 26

The query is in class.core.php and the connection in global.php. The Class.core is included like this:

(global.php)

foreach(glob(RUBY_BASE . '/app/includes/classes/class.*.php') as $class){
    include_once($class);
}

Any answers? `

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
LisaW
  • 171
  • 2
  • 3
  • 11
  • Are you requiring? i'm not sure about include, but `require_once` also make sure if you declare on the same file, use the `global` keyword so that PHP knows it's a variable that already exists, and not one within the scope of the function. – CodeTrooper May 16 '14 at 16:39
  • Does this answer your question? [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Nico Haase Jun 23 '23 at 06:43

4 Answers4

9

The variable $conn is not in scope for your class methods. You need do one of the following :

A.) pass the $conn variable into the method you want to call.

 public function getSitename($conn) {
        $stmt = $conn->prepare("SELECT value FROM cms_options WHERE title = 'sitename' ");
        $db->stmt_init();
         //and so on...
}

B.) Establish the connection inside each method (not good choice because you're not reusing an established connection)

C.) Make the connection variable global with a static definition. Could be set in the constructor of the class for example:

   public function __construct($conn) {
       if(empty(static::$conn) {
           static::$conn = $conn;
       }
   }

   public function getSitename() {
       $stmt = static::$conn->prepare("SELECT value FROM cms_options WHERE title = 'sitename' ");
        //... and so on

There are many other variations like these, but they are the general approaches

Ray
  • 40,256
  • 21
  • 101
  • 138
  • First solution gives me `Welkom op Warning: Missing argument 1 for Core::getSitename(), called in C:\xampp\htdocs\ruby\app\themes\default\index.php on line 118 and defined in C:\xampp\htdocs\ruby\app\includes\classes\class.core.php on line 25` – LisaW May 16 '14 at 17:35
  • Second solution = bad idea. Third idea gies me this: `Warning: Missing argument 1 for Core::__construct(), called in C:\xampp\htdocs\ruby\app\includes\classes\class.core.php on line 42 and defined in C:\xampp\htdocs\ruby\app\includes\classes\class.core.php on line 8 Fatal error: Access to undeclared static property: Core::$conn in C:\xampp\htdocs\ruby\app\includes\classes\class.core.php on line 10` – LisaW May 16 '14 at 17:38
  • @LisaW you have to alter your calls to the various function to pass in the $conn variable. Either when you do a `new YourThing($conn)` if you use the constructor (example C.) or when you call the method `$object->getSiteName($conn)` (example A) – Ray May 16 '14 at 18:16
  • Both of your answers don't work. Thanks for the answers trough – LisaW May 19 '14 at 14:27
  • Answer A worked for me just now. Thanks a lot. @LisaW, I think you didn't add "$conn" again when calling the function. – Jonathon Philip Chambers Sep 11 '18 at 17:21
0

This due to variable scope, $conn is defined outside the function, so or you pass it as parameter, or you set it as global, or you use an anonymous function.

$getSitename = function() use($conn) {
            $stmt = $conn->prepare("SELECT value FROM cms_options WHERE title = 'sitename' ");
            $db->stmt_init();
            $stmt->execute();
            $stmt->bind_result($sitename);
            if($stmt->num_rows > 0) {
                while ($stmt->fetch) {
                    return $sitename;
                }
            }
}
// Uses
$getSitename();
Ad Kahn
  • 551
  • 4
  • 6
-1

If $conn is initialized in the same file or in any other file and is included in the file which defines the getSitename() function, then you can mark the $conn variable as gloabl inside the function and it will work.

public function getSitename() {

            global $conn; 

            $stmt = $conn->prepare("SELECT value FROM cms_options WHERE title = 'sitename' ");
            $db->stmt_init();
            $stmt->execute();
            $stmt->bind_result($sitename);
            if($stmt->num_rows > 0) {
                while ($stmt->fetch) {
                    return $sitename;
                }
            }
        } 
Ghost-Man
  • 2,179
  • 1
  • 21
  • 25
-3
public function getSitename() {
    $stmt = $conn->prepare("SELECT value FROM cms_options WHERE title = 'sitename' ");
    $db->stmt_init();
    $stmt->execute();
    $stmt->bind_result($sitename);
    if($stmt->num_rows > 0) {
        while ($stmt->fetch) {
            return $sitename;
        }
    }
}
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
  • 1
    Please don't just dump code in an answer. Add some explanation to your answer such that others can learn from it, especially if the code looks exactly like the one from the problem statement – Nico Haase Jun 23 '23 at 06:44