duan205571 2017-02-17 06:29
浏览 29
已采纳

too long

I have a page where user input check in runtime that the description is exits in database or not and give a message, but when I run the code I am getting this error:

Catchable fatal error: Object of class mysqli could not be converted to string in C:\xampp\htdocs\VSC\dbcontroller.php on line 21

My dbcontroller.php code is:

<?php
class DBController {
    private $host = "localhost";
    private $user = "root";
    private $password = "";
    private $database = "voipswitch";

    function __construct() {
        $conn = $this->connectDB();
        if(!empty($conn)) {
            $this->selectDB($conn);
        }
    }

    function connectDB() {
        $conn = mysqli_connect($this->host,$this->user,$this->password);
        return $conn;
    }

    function selectDB($conn) {
        mysqli_select_db($this->$conn,database);// Line no 21
    }

    function runQuery($query) {
        $result = mysqli_query($query);
        while($row=mysqli_fetch_assoc($result)) {
            $resultset[] = $row;
        }       
        if(!empty($resultset))
            return $resultset;
    }

    function numRows($query) {
        $result  = mysqli_query($query);
        $rowcount = mysqli_num_rows($result);
        return $rowcount;   
    }
}
?>

I also paste check_availability.php code if you need this one also.

    <?php
//error_reporting(0);
require_once("dbcontroller.php");
$db_handle = new DBController();


if(!empty($_POST["enum_description"])) {
  $result = mysqli_query("SELECT count(*) FROM enumroots WHERE description='" . $_POST["enum_description"] . "'");
  echo $result;
  //die();
  $row = mysqli_fetch_row($result);
  $user_count = $row[0];
  //$user_count = $row['id_route'];
  echo $user_count;

  if($user_count>0) {
      //die('xxx');
      echo "<span class='status-not-available'> Description Not Available.</span>";
  }else{
      //die('yyy');
      echo "<span class='status-available'> Description Available.</span>";
  }
}
?>
  • 写回答

1条回答 默认 最新

  • dpvm7231 2017-02-17 06:41
    关注

    As you are storing the value in a private field called $database your selectDB function should access the value using $this->database.

    function selectDB($conn) {
        mysqli_select_db($conn, $this->database);// Line no 21
    }
    

    UPDATE: Your class is highly confused about where to store things. You can't mix and match $conn (method local) and $this->conn (class level). They are not the same thing.

    Please review your class and make a concious decision on whether to use a method level variable, or a property on your class, for each of your variables.

    If you review the code in my answer, you'll see it uses the argument $conn that is passed to the method. This will have a value, unlike $this->conn, which has never been assigned (effectively this is the opposite of the problem you had using $database which didn't exists rather than $this->database which does).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan