donglei1973 2015-05-13 02:43
浏览 38
已采纳

试图让一个类的函数在另一个函数内部工作是不成功的

I'm trying to get a function inside of a class to work with another function with no success.

Here is my code:

<?php
class db {

    public function dbLogin() {
        $mysqli = new mysqli("127.0.0.1","user","passowrd","database");

        if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
        }
        echo $mysqli->host_info . "
";
    }

    public function selectQuery() {
        $this->dbLogin();
        $query = "
            SELECT      column
            FROM        table
        ";

        if ($mysqli->query($query)) {
            echo "success";
        }       
    }
}

$fData = new db();
$fData->dbLogin();
$fData->selectQuery();
?>

The database connection works fine. I cannot get anything to echo once the query line runs, though.

What I've tried so far:

-Replacing the dbLogin() function with a construct() function, then getting rid of the $this->dbLogin line and $fData->dbLogin() line. This did not work.

-Moving this code around in different variations, just in case my order was wrong. That did nothing.

-Googling/looking at similar questions on StackOverflow with no luck.

I can do this procedural style easily, but it's time for me to move away from that. I am sure that there are much better ways to connect to a database using an OOP design pattern, but I am brand new to OOP so I am trying to use this as a lesson, and to gain a better understanding as to how functions interact with each other inside of a class. I know that I can throw the connection and query in one function, but then I would not know how to make functions interact with one another inside of a class.

How do I run the query in the selectQuery function based on the login information in the dbLogin function?

  • 写回答

2条回答 默认 最新

  • duanpuchun5275 2015-05-13 02:53
    关注

    You need to define your $mysqli outside the function, the problem with your code is related to global and local scope.

     <?php
     class db {
    private $mysqli;
    public function dbLogin() {
            $this->mysqli = new       mysqli("127.0.0.1","user","passowrd","database");
    
        if ($this->mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $this->mysqli->connect_errno . ") " . $mysqli->connect_error;
        }
        echo $mysqli->host_info . "
    ";
    }
    
    public function selectQuery() {
        $this->dbLogin();
        $query = "
            SELECT      column
            FROM        table
        ";
    
        if ($this->mysqli->query($query)) {
            echo "success";
        }       
    }
    }
    
    $fData = new db();
    $fData->dbLogin();
    $fData->selectQuery();
    ?>
    

    this will work

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大