update query sql table in yii - database

How can I update row in table in yii? I am using the following code but it is not working
$sql = "UPDATE auth_assignment SET itemname = 'Authenticated' WHERE userid = $user->accountID";
$command = $connection->createCommand($sql);
$command->execute();

My guess is that $user is being converted to a string and thus the ->accountID is not working. You have two methods, one unsafe and one safe.
Unsafe - Add {} around the $user->accountID.
$sql = "UPDATE auth_assignment SET itemname = 'Authenticated' WHERE userid = {$user->accountID}";
Safer - Use a parametrized query:
$sql = "UPDATE auth_assignment SET itemname = 'Authenticated' WHERE userid = :userid";
$command = $connection->createCommand($sql);
$command->execute(array(':userid' => $user->accountID))

ok I forgot to put commas around
'user->accountID'

Related

Getting value from database custom table not working in wordpress

I have created a wordpress table with wp_compare_post with following column id, user_id, post_id, post_type. I have attached the image of the database.
Below is the coading for viewing data from database
function compare_counting(){
global $wpdb;
//get user id
$user_ID = get_current_user_id();
$sql = "SELECT * FROM $wpdb->compare_post WHERE user_id = '$user_ID '";
$results = $wpdb->get_results($sql) or die(mysql_error());
foreach( $results as $result ) {
echo $result->post_id.',';
}
}
But getting following error.
Notice: Undefined property: wpdb::$compare_post in
I think it could be because you need to use $wpdb->prefix
Try this:
$sql = "SELECT * FROM ".$wpdb->prefix."compare_post WHERE user_id = '$user_ID'";

How to bind pdo key and value from Question mark?

I want to make a query select data :
$pdo = new PDO($dsn, $user, $pass, $options);
$query = "SELECT ? FROM ? WHERE ? = ?";
$pdo->prepare($query);
How can I do it?
PDOpreapare statement is not support this query right row

Query execution in codeigniter

I have a situation where I need to fetch details of an employee from the database using his ID and display them in the browser.
$sql = "SELECT * FROM employeesalarypayment WHERE empid = ".$val['empid'].";";
$query = $this->db->query($sql);
These are the statements that I have written to get the result array. My problem is how do I take a single field/column from this array? Also have I done it correctly?
Thanks in advance.
If your query return single data from database then you need to use
$row = $query->row_array()
Try this
$sql = "SELECT * FROM employeesalarypayment WHERE empid = ".$val['empid'].";";
$result = $this->db->query($sql)->result_array();
echo $result[0]['field_name'];

perl DBI execute doesn't recognize '?'

I have this code:
if($update[$entity_id])
{
my $sql = "UPDATE cache SET date = '?', value = '?' WHERE item_id = ? AND level = ? AND type = ?;";
}
else
{
my $sql = "INSERT INTO cache (date, value, item_id, level, type) VALUES ('?','?',?,?,?);";
}
my $db = $self->{dbh}->prepare(q{$sql}) or die ("unable to prepare");
$db->execute(time2str("%Y-%m-%d %X", time), $stored, $entity_id, 'entity', 'variance');
But when it want to run the update I get this error:
DBD::Pg::st execute failed : called with 5 bind variables when 0 are needed.
Why?
If you had turned on strict and/or warnings, you would see what your problem is.
You're writing
if (...) {
my $sql = ...;
} else {
my $sql = ...;
}
execute($sql);
Which means that the $sql variables that you declare in the if branches aren't in scope and you're trying to execute completely empty SQL.
You're preparing literal '$sql', but that is not your only problem, lexical $sql variables go out of scope outside {}.
Try,
use strict;
use warnings;
#...
my $sql;
if($update[$entity_id])
{
$sql = "UPDATE cache SET date = ?, value = ? WHERE item_id = ? AND level = ? AND type = ?";
}
else
{
$sql = "INSERT INTO cache (date, value, item_id, level, type) VALUES (?,?,?,?,?)";
}
my $st = $self->{dbh}->prepare($sql) or die ("unable to prepare");
$st->execute(time2str("%Y-%m-%d %X", time), $stored, $entity_id, 'entity', 'variance');

ZF Db Append to column

Im trying to make an update similar to this with Zend_Db:
UPDATE `TABLE` SET
column = column + 'new value'
WHERE
foo = 'bar'
any of you have done this before? is it possible?
Thanks
With the help of Zend_Db_Expr this is possible.
Example:
$newValue = 101;
$data = array('column' =>
new Zend_Db_Expr($db->quoteInto('column + ?', $newValue)));
$where = $db->quoteInto('foo = ?', 'bar');
$updated = $db->update('TABLE', $data, $where);
Resulting Query:
UPDATE `TABLE` SET `column` = `column` + 101 WHERE `foo` = 'bar';
If you are asking how to append a string, then the code is similar, but you cannot use the + operator when dealing with character data, instead use CONCAT().
For example, change the $data array to this:
$data = array('varcharCol' =>
new Zend_Db_Expr(
$db->quoteInto('CONCAT(varcharCol, ?)', ' append more text!')
));

Resources