php - PHP 中的键值对数组语法
问题描述
我已经使用 PDO 编写了一些 PHP 代码。我是 PHP 语法的新手,所以我正在关注 PHP 文档。我在 $params = array(... 语句上遇到语法错误。也许语法问题是对全局变量的引用,但这是我在 PHP 文档中找到的引用。
这是我的代码:
<?php
/*
Sets up a PDO object for the ResDatLog table
*/
class Resdatalog
{
// database connection and table name
private $_conn;
private $_table_name = "ResDataLog";
// object properties
public $ID;
public $Location = null;
public $Temperature;
public $Humidity;
public $Pressure;
public $RecDate;
public $AmbientTemp;
public $AmbientHum;
public $AmbientPressure;
/*
constructor with $db as database connection
*/
public function __construct($db)
{
$this->conn = $db;
}
/*
Reads all columns of the entire table ($table_name)
*/
function read()
{
// select all query
$query = "SELECT *
FROM
" . $this->table_name . "
ORDER BY
id DESC";
// prepare query statement
$stmt = $this->conn->prepare($query);
// execute query
$stmt->execute();
return $stmt;
}
function insert()
{
// insert a new row into the table_name table
$sql = 'INSERT Location, Temperature, Humidity, Pressure, RecDate,
AmbientTemp, AmbientHum, AmbientPressure into '
. $this->table_name .
' VALUES (:Location,:Temperature, :Humidity,
:Pressure, :RecDate, :AmbientTemp, :AmbientHum, :AmbientPressure)';
$sth = $this->conn->prepare($sql);
$params = array(
':Location' => ResDataLog->Location,
':Temperature'=> ResDataLog->Temperature,
':Humidity'=> ResDataLog->Humidity,
':Pressure'=> ResDataLog->Pressure,
':RecDate'=> ResDataLog->RecDate,
':AmbientTemp'=> ResDataLog->AmbientTemp,
':AmbientHum'=> ResDataLog->AmbientHum,
':AmbientPressure'=> ResDataLog->AmbientPressure);
Return($$sth->execute($params));
}
}
?>
解决方案
$this
您可以inside
使用类而不是使用直接类名来执行此操作。
如果您属于
outside
该类,那么您需要创建一个实例,Resdatalog
然后访问/分配值给变量,如下所示。$ResdatalogInstance = new Resdatalog; $ResdatalogInstance->Location="assign some values";
如果是
static
这样public static $Location;
,那么你可以这样做,Resdatalog::$Location
或者self::$Location
。$params = array( ':Location' => $this->Location, ':Temperature'=> $this->Temperature, ':Humidity'=> $this->Humidity, ':Pressure'=> $this->Pressure, ':RecDate'=> $this->RecDate, ':AmbientTemp'=> $this->AmbientTemp, ':AmbientHum'=> $this->AmbientHum, ':AmbientPressure'=> $this->AmbientPressure
);
您还需要$
从此行中删除额外的符号,
return ($sth->execute($params));
^^
推荐阅读
- java - 如何使用 XAdES4j 对 xml 进行签名,并引用 URI (#DatosEmision)?
- android - android片段视图即使在onCreateView中初始化后也会抛出空指针异常
- python - ValueError:从文本文件解析列表时出现格式错误的节点或字符串错误
- apache-spark - 根据 PySpark 中的时区将 UTC 时间戳转换为本地时间
- github-actions - 从 git 操作容器推送时,Git 推送操作不起作用
- java - Java最大函数递归
- google-apps-script - 谷歌表格脚本无法识别拆分功能
- database - 制定 MongoDB 查询
- reactjs - TypeError:无法读取未定义的属性“包含” - 成功编译后 chrome 中的错误
- ios - 如何在以下用例的通知扩展和应用程序之间进行通信?