Can't figure out why this code isn't working:
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute([$SQL_values]);
And these are dumps of the two strings being inserted into those statements:
$SQL_update = UPDATE laptops SET asset_tag = :asset_tag WHERE id = :id
$SQL_values = 'asset_tag' => 5544, 'id' => 23
You missed : in your code:-
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute([':asset_tag' => 5544, ':id' => 23]);
So actually what you have to do is:-
$SQL_values =[':asset_tag' => 5544, ':id' => 23]; // create array like this
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute($SQL_values); // pass that array
Or
$SQL_values =['asset_tag' => 5544, 'id' => 23]; // create array like this
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute($SQL_values); // pass that array
Note:- execute won't accept a string, it must be an array.
Related
The given syntax can insert the given values but i cannot retrieve the value with method. do anyone knoe how can i do that?
$supplier = array(
'cname' => $request->getPost('cname'),
'clname' => $request->getPost('clname'),
'tlidt' => $request->getPost('tlidt'),
'tledt' => $request->getPost('tledt'),
'pname' => $request->getPost('pname'),
'tla' => $request->getPost('tla'),
);
$builder = $db->table('suppliers');
$builder->insert($supplier);
$id = $this->$db->insert_id();
Try inserting like that
$this->db->insert('suppliers',$supplier);
$id = $this->db->insert_id();
or try removing $ sign from your last line 'db'.
My Query return always the Mysql HY093 error but why?
I have all parameters and still its not working...
Can someone help ?
// create PDO params array
$params = [
':employee ' => $formData['employee'],
':usedFrom' => $formData['usedFrom'],
':usedTill' => $formData['usedTill'],
':company' => $formData['company'],
':costCenter' => $formData['costCenter'],
':vehicle' => $formData['vehicle'],
':vehicleTag' => $formData['vehicleTag'],
':updatedAt' => $formData['updatedAt'],
':ID' => $formData['ID']
];
// update entry
$return = $db->pquery(
'UPDATE entry SET employee_id = :employee, used_from = :usedFrom, used_till = :usedTill, company_id = :company, cost_center_id = :costCenter, vehicle_id = :vehicle, vehicle_tag = :vehicleTag, updated_at = :updatedAt WHERE ID = :ID',
$params
);
Using the below, I echo a JSON array of the results. But this requires that I identify the column names which I'd like to return from the SQL query:
$new_sql = "SELECT TOP 200 * FROM STracker ORDER BY [ID] DESC";
$check_statement = sqlsrv_query($conn, $new_sql);
$data = array();
while($row = sqlsrv_fetch_array($check_statement, SQLSRV_FETCH_ASSOC)) {
$data['data'][] = array(
'id' => $row['ID'],
's_reference' => $row['s_reference'],
'reference' => $row['reference'],
'customer_name' => $row['customer_name']
);
}
Is there any way to create that array information, but return all of the columns returned by the query dynamically? So by using SELECT * FROM, all of the column data is returned in the array but without me needing to write out all of these individually? (the below)
'id' => $row['ID'],
's_reference' => $row['s_reference'],
'reference' => $row['reference'],
'customer_name' => $row['customer_name']
Ok I forgot to add that I'd tried this:
$data['data'][] = array($row);
Which is clearly wrong, and after using the following, it works perfectly!
$data['data'][] = $row;
How can I do update in doctrine, with array, so I don't do for loop for each time (I just want to do 1 call to database)
$myarray = [1, 2, 3];
$sql = "UPDATE `mytable` SET is_processing = :is_processing, end_time=NOW() WHERE id = :id";
$result = $this->connection->executeUpdate(
$sql,
array(
'is_processing' => false,
'id' => $myarray // This is unknown number amount of array
)
);
What I'm trying to achieve is:
it should update the table with field is_processing = false, endTime become current time, where id = whatever the array point to
Use IN clause in your query.
UPDATE `mytable` SET is_processing = :is_processing, end_time=NOW() WHERE id IN(:ids)
then
$result = $this->connection->executeUpdate(
$sql,
array(
'is_processing' => false,
'ids' => [3, 25]
),
array(
'ids' => \Doctrine\DBAL\Connection::PARAM_INT_ARRAY
)
);
This is the query I use:
$idList="32,33,21,11";
$query = "SELECT post_id, meta_value FROM wp_posts
WHERE meta_key = 'grade' AND post_id IN ($idList)";
As a result, the where clause become keys:
$result = array(
0 => array('post_id'=> 32, 'meta_value'=>5),
1 => array('post_id'=> 33, 'meta_value'=>2),
2 => array('post_id'=> 21, 'meta_value'=>8),
)
I desire to have a result like this:
$result = array(
0 => array( 32 => 5 ),
1 => array( 33 => 2 ),
2 => array( 21 => 8 ),
)
How to achieve this? Thanks!
Loop through the result set like this:
$_final = array(); // new result set
foreach ($result as $value)
$_final[] = array($value['post_id'] => $value['meta_value']);
unset($result); // free memory from old result set
UPDATE: If there are too many element and you afraid of memory consumption, you can do it the following way:
$_final = array(); // new result set
foreach ($result as $key => $value) {
$_final[] = array($value['post_id'] => $value['meta_value']); // composing new data
unset($result[$key]); // unsetting already parsed value
}
unset($result); // freeing memory from old result set