I have this mysql query:
UPDATE `table`.`wp_12_postmeta`
SET `meta_value` = 'yyy'
WHERE `wp_12_postmeta`.`meta_id` =5
LIMIT 1 ;
How do i incorporate this:
instead of wp_12_ i want a variable $prefix (variable holds wp_4_, wp_3_, etc)
instead of yyy i want a value $perf (variable is a name )
instead of 5 i want a value $meta_id (variable is a nr)
Thank u!
P.S.
here is what i use and it works:
$wpdb->query("UPDATE ".$prefix."postmeta SET meta_value = '".$perf."' WHERE meta_id = '".$meta_id."' LIMIT 1 ");
Problem is, when i execute this query, severl post meta fields are updated, instead of just one.
Ty
Use:
$query = sprintf("UPDATE `table`.`%s`
SET `meta_value` = '%s'
WHERE `%s`.`meta_id` = %d
LIMIT 1 ",
mysql_real_escape_string($prefix),
mysql_real_escape_string($perf),
mysql_real_escape_string($prefix),
mysql_real_escape_string($meta_id));
Here's how I would write this with PDO:
$prefix = "wp_4_";
$sql = "UPDATE `table`.`{$prefix}postmeta` SET `meta_value` = ?
WHERE `{$prefix}postmeta`.`meta_id` = ? LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($perf, $meta_id));
Related
How do I pass a sysdate to date column in an oracle table using oci_bind_by_name function
For example
$upquery = "UPDATE mytable SET fieldA = :b_ssn, fieldUpdated = :b_updateDate where fieldKey = :b_key";
$whatsup = oci_parse($conn, $upquery);
oci_bind_by_name($whatsup, ':b_ssn', $the_ssn, -1, OCI_B_INT);
oci_bind_by_name($whatsup, ':b_updateDate, sysdate, ... ? // <--
oci_bind_by_name($whatsup, ':b_key', $the_key, -1, OCI_B_INT);
oci_execute($whatsup);
How do i pass it?
ignore this... why not use sysdate in the query LOL
Do it like this:
$upquery = "UPDATE mytable SET fieldA = :b_ssn, fieldUpdated = SYSDATE where fieldKey = :b_key";
I am not familiar with php, perhaps this one works also:
oci_bind_by_name($whatsup, ':b_updateDate', new DateTime(), SQLT_ODT)
I am trying to create a check-in/check-out table in my database. My check-in form works without issue, inserting the time into my database. The problem occurs when I try to check out. Everything is good on the first entry...
But when I try to check in and check out again, this happens...
So far so good, but when I check out...
Currently, my code updates the out column and totalTime column of all matching child_id's.
Here is my code:
// Select the correct child from the database
$sql_childID = "SELECT id FROM child
WHERE firstName = '$childFirstName'
AND lastName = '$childLastName'";
$result = $pdo->query($sql_childID);
$row = $result->fetch();
$var = $row['id'];
// Insert the check out time for the child
$query = "UPDATE checkinout
SET `out` = :nowTime
WHERE child_id = $var
AND `in` IS NOT NULL";
$statement = $pdo->prepare($query);
$statement->bindValue(':nowTime', date("YmjHis"));
$statement->execute();
// Select check in time for specified child
$sql_inTime = "SELECT `in` FROM checkinout
WHERE child_id = $var";
$inResult = $pdo->query($sql_inTime);
$inRow = $inResult->fetch();
$inTime = strtotime($inRow['in']);
// Select the check out time for specified child
$sql_outTime = "SELECT `out` FROM checkinout
WHERE child_id = $var";
$outResult = $pdo->query($sql_outTime);
$outRow = $outResult->fetch();
$outTime = strtotime($outRow['out']);
// Find total hours
$totalTime = abs($outTime - $inTime)/(60*60);
// Update totalHours column for specified child
$queryTotalTime = "UPDATE checkinout
SET totalTime = :totalTime
WHERE child_id = $var
AND 'out' IS NOT NULL";
$statement = $pdo->prepare($queryTotalTime);
$statement->bindValue(':totalTime', $totalTime);
$statement->execute();
I think you could do all of this in your first update statement using TIMESTAMPDIFF rather than figuring the total time with PHP:
UPDATE checkinout
SET
out = NOW(),
totalTime = TIMESTAMPDIFF(SECOND, `in`, NOW()) / 3600
WHERE
child_id = $var
AND out IS NULL
The criteria WHERE out IS NULL will only update rows that do not have a value in the out column yet.
IF you have MySQL Db THEN sql will be
SELECT TIMESTAMPDIFF(HOUR,in,out) from checkinout;
I am sending data from an android app to save in my mysql database.
My Codes are
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = $time, transaction_status = 2 , remark = $rem WHERE transaction_id = $t_id LIMIT 1");
And my dabase is as,
remark varchar(500) latin1_general_ci.
The problem is whenever I have $rem as a numbric value then database is updated but if I use any text then it wont.
Please help me.
Make sure wrapping string data with single quote.
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = '$time', transaction_status = 2 , remark = '$rem' WHERE transaction_id = $t_id LIMIT 1")
Text should be wrapped by single quotes: ''
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = $time, transaction_status = 2 , remark = '$rem' WHERE transaction_id = $t_id LIMIT 1");
$saa = "update aspirantdt set vote = 'vote'+1 where 'post_id' = '$id' ";
When i check the database the value didnt increase.
Help me out.
yuo have a sintax mistake in your update query, you are using quotest instead of backtick. Use backticks for column names and quotes for values try to change as follow
$saa = "update `aspirantdt` set `vote` = (`vote`+1) where `post_id` = '".$id."' ";
Just remove quotes or use Backtics for column names.
$saa = "UPDATE aspirantdt SET vote = vote + 1 where post_id = '$id' ";
Or with backticks
$saa = "UPDATE `aspirantdt` SET `vote` = `vote` + 1 where `post_id` = '$id' ";
$saa = "update aspirantdt set vote = 'vote'+1 where 'post_id' = '$id' ";
means that 'vote' and 'post_id' are literal strings, not table names (that is, it will compare $id to the actual string post_id instead of the value of the post_id column).
What you want is backticks to quote them as a column/table name instead;
$saa = "update `aspirantdt` set `vote` = `vote`+1 where `post_id` = '$id' ";
You don't need quotes around vote as its a column name, not a string.
Your SQL is probably trying to put vote1 in an int column.
I want t make a zend update query.
I was looking at zend documentation but I didn't see any example like the one I want.
The question its if may someone could help me with the query.
I want to make the next query:
Update cars set active = 0
Where id in (SELECT idCar FROM UserCars Where idUser=3)
Simplest way to run any complex query. There can be some better ways to do same.
$sql = "Update cars set active = 0
Where id in (SELECT idCar FROM UserCars Where idUser=3)";
$query = $this->getDbTable()->getAdapter()->query($sql, $data);
$query->execute();
Try this too
$data = array { 'active' = '0' };
$where = "id in (SELECT idCar FROM UserCars Where idUser=3)";
$db->update($data, $where);
Perhaps more Zendy way:
$idUser = 3;
$sub_select
= $db->select()
->from('UserCars', array('id'))
->where('idUser = ?', $idUser);
$updated_rows
= $db->update('cars',
array('active' => 0),
"id IN ($sub_select)"
);