buongiorno
DOMANDA:
utilizzare una variable anzichè una stringa come primo argomento di cioè trasformare questo:
Codice:
$stmt->bindParam(":variable_name",$variable_value,PDO::PARAM_STR);
in questo:
Codice:
$stmt->bindParam($variable_name,$variable_value,PDO::PARAM_STR);
Vi cerco di illustrare:
attraverso questo codice io carico dati in ingresso sul database FUNZIONA
Codice PHP:
<?php
require_once '../shared/Logger.php';
require_once '../shared/Temperature_Config0.1.php';
class DBManager {
private $dbh;
//print(":weigth1");
const _SAVE_DATA = "INSERT INTO apidata(temperature,luminosity,humidity,pressure) VALUES(:temperature,:luminosity,:humidity,:pressure)";
const _SELECT_TEMPERATURE = "SELECT temperature,date_format(date_time, '%k:%i') Date FROM `apidata`";
public function __construct($conn) {
$this->dbh = $conn;
}
/**
* This function saves the temperature value on the db
* @param string $temperature
* @throws Exception is used in case of exception
*/
public function save_data($temperature,$luminosity,$humidity,$pressure,$weights) {
try {
echo("start");
$stmt = $this->dbh->prepare(self::_SAVE_DATA);
$stmt->bindParam(":temperature",$temperature,PDO::PARAM_STR);
$stmt->bindParam(":luminosity",$luminosity,PDO::PARAM_STR);
$stmt->bindParam(":humidity",$humidity,PDO::PARAM_STR);
$stmt->bindParam(":pressure",$pressure,PDO::PARAM_STR);
$stmt->execute();
} catch (PDOException $ex) {
Logger::log($ex->getCode(), $ex->getMessage());
throw new Exception("Failed save luminosity");
}
}
/**
* This function recovers the temperature values
* @throws Exception is used in case of exception
* @return $result is a array with the result of the query
*/
public function select_temperature() {
try {
$stmt = $this->dbh->prepare(self::_SELECT_TEMPERATURE);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
} catch (PDOException $ex) {
Logger::log($ex->getCode(), $ex->getMessage());
throw new Exception("Impossible load last 5 posts, there was an error");
}
}
}
?>
ora devo inserire dei dati che sono in una lista, sono stringhe così formate:
Codice PHP:
$weights = (1w123,2w456,3,789)
quindi ho inserito questa parte di codice
Codice PHP:
foreach( $weights as $value)
{
//echo $value."<br />";
$weight = explode('w',$value);
$index = $weight[0];
$weight = $weight[1];
echo (($index)."<br />");
//$column = ":weight"."1";
if ($index != ""){
$column = ":weight".$index;
$stmt->bindParam($column, $weight,PDO::PARAM_STR);
echo($column." ".$weight."<br />");
}
}
ed ho ottenuto questo che però NON FUNZIONA:
Codice PHP:
<?php
require_once '../shared/Logger.php';
require_once '../shared/Temperature_Config0.1.php';
class DBManager {
private $dbh;
//print(":weigth1");
const _SAVE_DATA = "INSERT INTO apidata(temperature,luminosity,humidity,pressure,weight1) VALUES(:temperature,:luminosity,:humidity,:pressure,:weight1)";
const _SELECT_TEMPERATURE = "SELECT temperature,date_format(date_time, '%k:%i') Date FROM `apidata`";
public function __construct($conn) {
$this->dbh = $conn;
}
/**
* This function saves the temperature value on the db
* @param string $temperature
* @throws Exception is used in case of exception
*/
public function save_data($temperature,$luminosity,$humidity,$pressure,$weights) {
try {
echo("start");
$stmt = $this->dbh->prepare(self::_SAVE_DATA);
foreach( $weights as $value)
{
//echo $value."<br />";
$weight = explode('w',$value);
$index = $weight[0];
$weight = $weight[1];
echo (($index)."<br />");
//$column = ":weight"."1";
if ($index != ""){
$column = ":weight".$index;
$stmt->bindParam($column, $weight,PDO::PARAM_STR);
echo($column." ".$weight."<br />");
}
}
$stmt->bindParam(":temperature",$temperature,PDO::PARAM_STR);
$stmt->bindParam(":luminosity",$luminosity,PDO::PARAM_STR);
$stmt->bindParam(":humidity",$humidity,PDO::PARAM_STR);
$stmt->bindParam(":pressure",$pressure,PDO::PARAM_STR);
$stmt->execute();
} catch (PDOException $ex) {
Logger::log($ex->getCode(), $ex->getMessage());
throw new Exception("Failed save luminosity");
}
}
/**
* This function recovers the temperature values
* @throws Exception is used in case of exception
* @return $result is a array with the result of the query
*/
public function select_temperature() {
try {
$stmt = $this->dbh->prepare(self::_SELECT_TEMPERATURE);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
} catch (PDOException $ex) {
Logger::log($ex->getCode(), $ex->getMessage());
throw new Exception("Impossible load last 5 posts, there was an error");
}
}
}
?>
Grazie per l'attenzione