Notice: Undefined variable: conn

2019-07-03 14:57发布

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

标签: php mysqli
3条回答
甜甜的少女心
2楼-- · 2019-07-03 15:34

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

查看更多
贪生不怕死
3楼-- · 2019-07-03 15:40

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();
查看更多
贼婆χ
4楼-- · 2019-07-03 15:47

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;
                }
            }
        } 
查看更多
登录 后发表回答