My Code :
$item = new Items2;
$connection = $item->getReadConnection();
$sql = "CALL `OLViewItemsListViewAllWithCount`('','','',0,0,0,0,0,0,0,'2018-05-15',0,0,10,#aa);";
$items = new Resultset(null,$robot,$connection->query($sql));
i need to get the #aa from this Result as a output parameter
how can i get it ?
Need to select out parameters in second query like..
$item = new Items2;
$connection = $item->getReadConnection();
$sql = "CALL `OLViewItemsListViewAllWithCount`('','','',0,0,0,0,0,0,0,'2018-05-15',0,0,10,#aa);";
$items = $robot,$connection->query($sql)->fetchAll(\Phalcon\Db::FETCH_ASSOC);;
$sql2 = "SELECT #aa";
$aa = $connection->query($sql2)->fetch();
echo $aa['#aa];
Related
I receive this error:
ExecuteSqlQuery : Must declare the scalar variable "#date".
I call the query as such: ExecuteSqlQuery -server "sqlbox" -database "dbname" and I receive the date variable error. You can see my query statement uses the variable m.created_date >= #date.
How do I correctly pass the date variable to query string so it runs in the function without an issue? NOTE: the code works when I hardcode the date value.
Function ExecuteSqlQuery {
Param(
[Parameter(Mandatory=$true)][string]$server,
[Parameter(Mandatory=$true)][string]$database
)
Process
{
$date= $((get-date).AddSeconds(-120).ToString("MM-dd-yyyy HH:mm:ss"))
$getscriptinfo = "select m.created_date, a.engine_full_hierarchy as Location, e.exciter_name, e.engine_exciter_id as ExciterID, m.additional_data as ReasonDown from HugsAdminAmador.mv_audit m, HugsAdminAmador.exciter e, HugsAdminAmador.area_map a where m.object_id = e.exciter_id and e.area_map_id = a.area_map_id and m.created_date >= #date and m.additional_data like '%NewStatus=DOWN%' and m.additional_data not like '%autonomous%'"
$scriptscon = New-Object System.Data.SqlClient.SqlConnection
$scriptscon.ConnectionString = "Data Source=$server;Initial Catalog=$database;Integrated Security=true"
$scriptcmd = New-Object System.Data.SqlClient.SqlCommand
$scriptcmd.Connection = $scriptscon
$scriptcmd.CommandText = $getscriptinfo
$scriptcmd.CommandTimeout = 0
$ErrorActionPreference = 'Stop'
try
{
$scriptscon.Open()
$Reader = $scriptcmd.ExecuteReader()
# if reader returns data create an array of the error data for the potential alert.
If ($Reader.HasRows) {
$obj = $Reader | foreach {
$row = $_;
#the left naming is for the column headers in the email.
new-object psObject -Property #{
CreateDate = $row.Item("created_date")
ReasonDown = $row.Item("ReasonDown")
ObjectID = $row.Item("object_id")
}
}
}
return $obj
}
catch [Exception]
{
# Write-Warning "Get-ExecuteScripts (Connection: [$server].[$database])"
# Write-Warning $_.Exception.Message
# Write-Warning "Query: $getscriptinfo --Id $scriptid"
Write-Error $_
$ErrorEvent = #{
LogName = 'Exciter_Log'
Source = 'Exciter_Health'
EventID = 333
EntryType = 'Information'
Message = $_
}
Write-EventLog #ErrorEvent
}
finally
{
$ErrorActionPreference = 'Continue'
$scriptscon.Dispose()
$scriptcmd.Dispose()
}
}
}
I´ve got the following SQL-Query
$uidArray = explode(",", $uids);
foreach ($uidArray as $uid) {
$dynamicUid[] = '`uid` LIKE \''.$uid.'\'';
}
$query = $this->createQuery();
$query->statement("SELECT * FROM `tx_myextension_domain_model_thi` WHERE ".implode($dynamicUid, " OR "));
return $query->execute();
This works fine but I want to have it like this:
$uidArray = explode(",", $uids);
$query = $this->createQuery();
foreach ($uidArray as $key => $value) {
$constraints[] = $query->equals('uid', $value);
}
return $query->matching(
$query->logicalAnd($constraints)
)->execute();
Here I get the following Output with the Query Parser :
'SELECT `tx_myextension_domain_model_thi`.* FROM `tx_myextension_domain_model_thi`
`tx_myextension_domain_model_thi`
WHERE ((`tx_myextension_domain_model_thi`.`uid` = :dcValue1) AND
(`tx_myextension_domain_model_thi`.`uid` = :dcValue2)) AND
(`tx_myextension_domain_model_thi`.`sys_language_uid` IN (0, -1)) AND
(`tx_myextension_domain_model_thi`.`pid` = 0) AND
((`tx_myextension_domain_model_thi`.`deleted` = 0) AND (
`tx_myextension_domain_model_thi`.`t3ver_state` <= 0) AND
(`tx_myextension_domain_model_thi`.`pid` <> -1) AND
(`tx_myextension_domain_model_thi`.`hidden` = 0) AND
(`tx_myextension_domain_model_thi`.`starttime` <= 1607084460) AND
((`tx_myextension_domain_model_thi`.`endtime` = 0)
OR (`tx_myextension_domain_model_thi`.`endtime` > 1607084460)))'
And the Uids as dcValue-Array.
dcValue1 => '1' (1 chars)
dcValue2 => '2' (1 chars)
Maybe someone can help me to rewrite this, because unfortunately I can't get any further!
Thanks :)
Did you try the in operator?
public function yourFunctionName($uid)
{
$query = $this->createQuery();
$query->in('uid', $uidArray);
return $query->execute();
}
Assuming that your array looks like this:
$uidArray = [
0 = '34',
1 = '15',
3 = '88'
]
EDIT
If you do not care about where your objects are stored then you can do the following.
public function yourFunctionName($uid)
{
$query = $this->createQuery();
$query->getQuerySettings()->setRespectStoragePage(FALSE);
$query->matching(
$query->in('uid', $uidArray)
);
return $query->execute()];
}
Which is going to ignore the pid in the query
How can i add a value to an existing value in SQL example a value 4 in a column how can i add 2 to it to make it 6 not to update to 2 but to add the previous value and the new value to together here is my class php file
<?php
class data
{
public $d_count;
public $id;
private $conn;
private $table_name;
public function __construct($db)
{
$this->conn = $db;
$this->table_name= "tbl_data";
}
public function updateCount()
{
// $query = "UPDATE tbl_data SET d_count = ?, date_updated = ? WHERE id = ?";
$query = "UPDATE tbl_data SET d_count '+1' = ?, date_updated = ? WHERE id = ?";
$obj = $this->conn->prepare($query);
$this->d_count = htmlspecialchars(strip_tags($this->d_count));
$this->date_updated = htmlspecialchars(strip_tags($this->date_updated));
$this->id = htmlspecialchars(strip_tags($this->id));
$obj->bind_param("sss", $this->d_count, $this->date_updated, $this->id);
if ($obj->execute()) {
return true;
}
return false;
}
}
The line I commented out is what i used to test the code and it works fine but now i need to add the old value + new value together here is the code am using to update the table
<?php
$data = new data($connection);
if($_SERVER['REQUEST_METHOD'] === "POST"){
$newDCount = json_decode(file_get_contents("php://input"));
if(!empty($newDCount->d_count))
$data->id= $newDCount->id;
$data->d_count = $newDCount->d_count;
$data->date_updated = date('Y-m-d H:i:s', time());
if($apiata->updateCount()){
http_response_code(200);
echo json_encode(array(
"status" => 200,
"message" => "Success"
));
}
else
{
http_response_code(200);
echo json_encode(array(
"status" => 500,
"message" => "Failed"
));
}
}
To update the d_count to add to the existing d_count value, the query should look like:
$sql = 'UPDATE catalog SET d_count = d_count + ?, date_updated = ? WHERE id = ?';
The other handling should be ok as-is.
I can't json_encode my array returned from PDO:
var_dump(json_encode($result)); returns false
I have also reassigned this array to a new array, looping through the PDO result.
Here is my code:
$host = "localhost";
$database = "test";
$username = "root";
$password = "";
$db = new PDO("mysql:host={$host};port:3306;dbname={$database}", $username, $password);
!$db && exit("Database connect failed");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $db->prepare("SELECT sample.* FROM test.sample LIMIT 10");
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
var_dump(json_encode($result));
I've found the same problem, found out that the $result itself is error/false.
you should check the content of your $result, using
print_r($result);
if it is so (still false), you should your database coneection or your syntax query.
I'd like to know if there is a way to insert multiple row in ZF2 using only one $sql object (and not using the query(SQL COMMAND) method).
I tried something like this, but it doesn't work:
public function setAgentProjectLink( $IDProject , $IDsAgents )
{
$values = array () ;
foreach ( $IDsAgents as $IDAgent):
{
$values[] = array ( 'id_agent' => $IDAgent , 'id_projet' => $IDProject) ;
} endforeach ;
$sql = new Sql( $this->tableGateway->adapter ) ;
$insert = $sql->insert() ;
$insert -> into ( $this->tableGateway->getTable() )
-> values ( $values ) ;
$statement = $sql->prepareStatementForSqlObject($insert);
$result = $statement->execute();
}
Trying to insert values in a database with two columns (id_agent, id_projet)
there is no generic way for multyinsert in ZF2 BUT if you are using mysql and not planning to change to other databases, i have written a multiInsert function for myself:
$data is an array of arrays of key,value pairs.
protected function multiInsert($table, array $data)
{
if (count($data)) {
$columns = (array)current($data);
$columns = array_keys($columns);
$columnsCount = count($columns);
$platform = $this->db->platform;
array_filter($columns, function ($index, &$item) use ($platform) {
$item = $platform->quoteIdentifier($item);
});
$columns = "(" . implode(',', $columns) . ")";
$placeholder = array_fill(0, $columnsCount, '?');
$placeholder = "(" . implode(',', $placeholder) . ")";
$placeholder = implode(',', array_fill(0, count($data), $placeholder));
$values = array();
foreach ($data as $row) {
foreach ($row as $key => $value) {
$values[] = $value;
}
}
$table = $this->db->platform->quoteIdentifier($table);
$q = "INSERT INTO $table $columns VALUES $placeholder";
$this->db->query($q)->execute($values);
}
}