dsakktdog498483070 2014-09-06 14:27
浏览 14
已采纳

php mysqli“没有数据库选择”使用类

When I'm using mysqli without class it's going ok:

index.php

require_once(dirname(__FILE__) . '/config.php');

$mysqli = new mysqli($hostname, $username, $password, $dbname);
$queryText = "SELECT * FROM User";
if($query = $mysqli->query($queryText)) {
    $results = $query->fetch_array();
    echo $results['userId'];
} else {
     echo "Error ";
     echo $mysqli->errno . " " . $this->mysqli->error;
}

?>

But when I start using mysqli with class something goes wrong. connectDB doesn't give any error, so i get connected to DB. But then when trying do any query it give me "No database selected error" Result of index.php is: Error 1046 No database selected

index.php

<?php
require_once(dirname(__FILE__) . '/banana.php');

$banana = new Banana(1);
if ($banana->connectDB()) {
    $banana->doQuery();
}
?>

banana.php

<?php

require_once(dirname(__FILE__) . '/config.php');

class Banana {

     private $mysqli, $userId, $query;

     function __construct($userId) {
          $this->userId = $userId;
     }

     function __destruct() {
        $this->mysqli->close();
     }

     public function connectDB() { // Подключение к БД
          $this->mysqli = new mysqli($hostname, $username, $password, $dbname);
          if ($this->mysqli->connect_errno) {
               echo "Error (" . $this->mysqli->connect_errno . ") " . $this->mysqli->connect_error;
               return false;
          }
          return true;
     }

     public function doQuery() {
        $queryText = "SELECT * FROM User";
        if($this->query = $this->mysqli->query($queryText)) {
            $results = $query->fetch_array();
            echo $results['userId'];
        } else {
            echo "Error ";
            echo $this->mysqli->errno . " " . $this->mysqli->error;
        }
    }

?>

So it's very frustrating. I'm about 2 weeks in php, but can't find answer for couple days. I guess the answer is obvious but I can't see it. Thank you for your time and patience.

  • 写回答

2条回答 默认 最新

  • dongtuo4132 2014-09-06 15:07
    关注

    One of the first problems you will encounter when you run your script is here:

     public function connectDB() { // Подключение к БД
          $this->mysqli = new mysqli($hostname, $username, $password, $dbname);
    

    Note that all 4 variables you are using in your function call ($hostname, etc.) are undefined in the scope of the method.

    There are several ways you can solve this:

    • Pass the variables as parameters to the method:
      public function connectDB($hostname, ...
    • Pass the necessary variable to your class constructor and set configuration properties in your class that you can use later on;
    • Use constants instead of variables;
    • Declare your variables global.

    I would recommend one of the first 2 and definitely not the last one.

    You can read more in the php manual about variable scope.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码