Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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
从SELECT语句返回的PHP打印特定值_Php_Mysql_Model View Controller_Pdo - Fatal编程技术网

从SELECT语句返回的PHP打印特定值

从SELECT语句返回的PHP打印特定值,php,mysql,model-view-controller,pdo,Php,Mysql,Model View Controller,Pdo,我在一个小网站上工作,有一步我有点困惑。 我遵循一个非常简单的mvc模式,我目前有一个控制器,它创建一个会话数组,保存所有发布的数据。如果表单已经提交,这些数据来自几个不同的下拉框,它们都有不同的ID: <?php require_once('../Model/CalculatorModel.php'); require_once('../Tool/DrawTool.php'); $newCalc = new ConCalc(); // instantiate drawing too

我在一个小网站上工作,有一步我有点困惑。 我遵循一个非常简单的mvc模式,我目前有一个控制器,它创建一个会话数组,保存所有发布的数据。如果表单已经提交,这些数据来自几个不同的下拉框,它们都有不同的ID:

 <?php
require_once('../Model/CalculatorModel.php');
require_once('../Tool/DrawTool.php'); 

$newCalc = new ConCalc();
// instantiate drawing tool
$draw = new DrawTool();
// parse (render) appliance view
$renderedView = $draw->render('../View/calculator.php', array('calcvalues' => $newCalc->getValues()));

if(isset($_POST['btn-calcCon'])){

$_SESSION['post-data'] = $_POST;
$_SESSION['post-data']['heatingType'];
$_SESSION['post-data']['meterType'];
$_SESSION['post-data']['noBedrooms'];
$_SESSION['post-data']['houseType'];
$_SESSION['post-data']['houseAge'];


    }

echo $renderedView;

?>
现在,我的表格顶部的两行如下所示:

HeatingType     MeterType   Bedrooms    HouseType   HouseAge              Consumption
Gas             Standard    1 or 2     Flat          Less than 11 years     5430
Gas             Standard    1 or 2     Flat          More than 11 years     7270
如果我选择Gas,Standard,1或2,Flat,小于11,则提交表单后,数字5430应显示在文本框中

我有点不确定如何返回那个值。我试图回显$stmt变量,但它说PHP无法回显PDO对象

任何帮助都将不胜感激 谢谢

请尝试以下代码:

while ($result = $stmt->fetch(PDO::FETCH_ASSOC))
  echo $result['gasUsed'].' '.$result['heatingType'] ;//...
编辑:
所以,如果您需要在结果中查找值(但我不知道为什么不使用where子句来筛选结果),请执行以下操作:

while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
  if( $result['gasUsed'] == 'Gas' && $result['heatingType'] == 'Standard' /* ... */ ){
    $v = $result['Consumption'];
    break;
  } 
}

语句具有
=
var_dump类似吗?是的,很抱歉我忘记更新我的var_dump,原始代码已被编辑。
可能重复($result=$stmt->fetch(PDO::fetch_ASSOC)){echo$result['Consumption'];}
似乎可以工作,但我希望能够将消费值存储在变量中,并在我看来调用该变量。我做得很好
while($result=$stmt->fetch(PDO::fetch_ASSOC)){$TypicalReading=$result['Consumption'];break;}
$TypicalReading
是我的模型类中的公共变量。当我在我的视图类中调用它时,我得到了未定义的变量error对不起,我的视图文件没有类,它是纯HTMLshow代码,在这里设置$TypicalReading,在那里使用$TypicalReading如何使用
getValues()
?尝试添加
返回$TypicalReading
getValues
函数。。。
while ($result = $stmt->fetch(PDO::FETCH_ASSOC))
  echo $result['gasUsed'].' '.$result['heatingType'] ;//...
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
  if( $result['gasUsed'] == 'Gas' && $result['heatingType'] == 'Standard' /* ... */ ){
    $v = $result['Consumption'];
    break;
  } 
}