duande1985 2019-07-31 07:54
浏览 93

我们可以在php类的单个方法中使用两个mysql查询吗?

I am wondering that whether it is possible to write two different mysql queries in one php class object method and use those results in an another class method. Here's I am trying to do (I put only the relevant part of my code here), but I think it's not working:

<?php

    public function sql()
    {
        $sql = "SELECT * FROM customers";

        // Another sql
        $sql_sales = "SELECT SUM(sales) as sales FROM customers";
        // Execute this sql and result is stored in a variable
        $this->sales = $row['sales'];

        return $sql;
    }

    public function customers_list()
    {
        $sql = $this->sql();

        $customers = '
        <div id="customers">
            <div id="customers_num"><span>'.$this->sales.'</span> Sales</div>
        </div>';
        return $customers;
    }

?>

Can we use value of variable $this->sales inside another method?

If not then what's the correct way of getting it's value?

  • 写回答

2条回答 默认 最新

  • dqhdz04240 2019-07-31 08:04
    关注

    In case your code is inside a class and you're using an instantiated object :

    From PHP basics

    The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object

    Therefore you can access any of your object properties inside its methods.

    In your specific case $this->sales = $row['sales']; stores a value in its sales property which can be used anywhere else in the object scope.

    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容