Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从一个函数中获取结果并将其输入到另一个函数中?_Php_Mysql - Fatal编程技术网

Php 从一个函数中获取结果并将其输入到另一个函数中?

Php 从一个函数中获取结果并将其输入到另一个函数中?,php,mysql,Php,Mysql,我有一个类和一个函数,我想做的是“返回”值并输入到另一个函数中 Class test1 { public function a($x) { $runquery = "Select * FROM testdb where color_id = '{$x}'"; $result = mysql_query($runquery) or die(mysql_error()); $base_results = mysql_fetch_array($result); $red

我有一个类和一个函数,我想做的是“返回”值并输入到另一个函数中

Class test1 {

public function a($x) {
  $runquery = "Select * FROM testdb where color_id = '{$x}'";
    $result = mysql_query($runquery) or die(mysql_error());
    $base_results = mysql_fetch_array($result);

    $red = $base_results['red'];

}

public function c($red) {
$runsecond_query = "SELECT * test2db where $color = '{$red}'";
// write additional code

}
好的,我真正想做的是从函数“a”得到结果,然后将结果输入函数“c”。我希望这是有道理的。提前感谢所有人

function a($x) {
  ....
  return $red;
}

c(a($x));
或者,如果您希望它更清晰一些:

$red = a($x);
c($red);

由于两个函数都是同一个类的成员,因此可以创建一个类属性来存储它们:

Class test1 {

    // Private property to hold results
    private $last_result;

    public function a($x) {
      $runquery = "Select * FROM testdb where color_id = '{$x}'";
        $result = mysql_query($runquery) or die(mysql_error());
        $base_results = mysql_fetch_array($result);

        // Store your result into $this->last_result
        $this->last_result = $base_results['red'];
    }

    public function c() {
      $runsecond_query = "SELECT * test2db where $color = '{$this->last_result}'";
      // write additional code
    }
}

对于所使用的示例,在MySQL中使用JOIN子句可能会容易得多,这样可以节省正在使用的函数数和查询数非常感谢!!这是greatI,我遇到了一个错误:当不在对象上下文中时使用$This——有什么解决方法吗?您正在实例化类
test1
?比如:
$t=newtest1()$t->a($x)$t->c()