Update DateTime manually - mysql

Since I'm hassling to much with the timestamp I would like to use dateTime. But since dateTime doesn't offer the auto update, I need to do it manually. But my NOW() doesn't do anything to my row.
function user_activity($active){
$query = $this->link->prepare("UPDATE memory SET
page = ? AND activity2 = NOW() WHERE user_id='{$_SESSION['id']}' ");
$values = array($active);
$query->execute($values);
$counts = $query->rowCount();
return $counts;
}

Don't use AND in your SET statement, instead use a comma. It also looks like you have too many ) in your query.
function user_activity($active) {
$query = $this->link->prepare("UPDATE memory SET page = ?, activity2 = NOW() WHERE user_id='{$_SESSION['id']}' ");
$values = array($active);
$query->execute($values);
$counts = $query->rowCount();
return $counts;
}

As Phil mentions, you have a query error here. Always use exception handling when executing SQL queries.
Learn Exception Handling

Related

MySQL optional filters for search query

I am working on a query that has an optional filter, so lets assume the table name is products and the filter is the id (primary key)
If the filter is not present I would do something like this:
SELECT * FROM products;
If the filter is present I would need to do something like this:
SELECT * FROM products WHERE id = ?;
I have found some potential solutions that can mix the 2 in sql rather than doing conditions in the back-end code itself
SELECT * FROM products WHERE id = IF(? = '', id, ?);
OR
SELECT * FROM products WHERE IF(? = '',1, id = ?);
I was just wondering which one would be faster (In the case of multiple filters or a very big table) Or is there a better solution to handle this kind of situation?
A better approach is to construct the WHERE clause from the parameters available. This allows the Optimizer to do a much better job.
$wheres = array();
// Add on each filter that the user specified:
if (! empty($col)) { $s = $db->db_res->real_escape_string($col);
$wheres[] = "collection = '$s'"; }
if (! empty($theme)) { $s = $db->db_res->real_escape_string($theme);
$wheres[] = "theme = '$s'"; }
if (! empty($city)) { $s = $db->db_res->real_escape_string($city);
$wheres[] = "city = '$s'"; }
if (! empty($tripday)) { $s = $db->db_res->real_escape_string($tripday);
$wheres[] = "tripday = '$s'"; }
// Prefix with WHERE (unless nothing specified):
$where = empty($wheres) ? '' :
'WHERE ' . implode(' AND ', $wheres);
// Use the WHERE clause in the query:
$sql = "SELECT ...
$where
...";
Simplest approach is OR:
SELECT *
FROM products
WHERE (? IS NULL OR id = ?);
Please note that as you will add more and more conditions with AND, generated plan will be at least poor. There is no fit-them-all solution. If possible you should build your query using conditional logic.
More info: The “Kitchen Sink” Procedure (SQL Server - but idea is the same)

Updating Multiple Column on MySQL

I want to update members_roosevelt table ACCOUNT column starting with 3000+ value I also want to update ACCOUNT column on loan_roosevelt table that is related to my member_roosevelt. What's wrong with my query? Thank you!
$query1 = "SELECT ACCOUNT
FROM
`members_roosevelt`";
$result_q1 = $link->query($query1) or die($link->error);
while ($obj = $result_q1->fetch_object()) {
$members[] = $obj->ACCOUNT;
}
$ids = implode(',', $members);
$sql = "UPDATE `members_roosevelt` as `memb`
JOIN `loan_roosevelt` as `loan`
ON `memb`.`ACCOUNT` = `loan`.`ACCOUNT`
SET
(`memb`.`ACCOUNT`,
`loan`.`ACCOUNT`) = CASE ACCOUNT";
foreach ($members as $id => $ordinal) {
$sql .= sprintf("WHEN %d THEN %d ", $ordinal, (3000+$id));
}
$sql .= "END WHERE memb.ACCOUNT IN ($ids)";
$link->query($sql) or die($link->error);
SET (`memb`.`ACCOUNT`, `loan`.`ACCOUNT`) = CASE ACCOUNT...
This is simply not part of SQL syntax. You can't set two columns at a time like this. The left side of an assignment operator must be one column.
A better solution is to use a session variable.
SET #acct = 3000;
UPDATE members_roosevelt as memb
JOIN loan_roosevelt as loan
ON memb.ACCOUNT = loan.ACCOUNT
SET memb.ACCOUNT = (#acct:=#acct+1),
loan.ACCOUNT = (#acct);
This way you don't have to run the SELECT query at all, and you don't have to create a huge UPDATE statement with potentially thousands of WHEN clauses.
Demo: SQLFiddle

Is there a variable for mysql that returns all?

I have a MySQL query using a WHERE clause based on a variable. I am trying to figure out if there is a way to set the variable to something like a wildcard that would return any value in that field?
WHERE c.call_state_id = $state
AND (follow_emp_id = $user_id OR follow_emp_id = 117)
ORDER BY $sort
Instead of limiting the results to 1 specific $user_id, I want to virtually eliminate the AND portion of the query.
I would build the query dynamically, and check for the value before putting it into the WHERE clause.
if (empty($userid)) {
$userid_check = ""
} else {
$userid_check = "AND (follow_emp_id = $user_id OR follow_emp_id = 117)"
}
$sql = "SELECT ...
WHERE c.call_state_id = $state
$userid_check
ORDER BY $sort";
See https://stackoverflow.com/a/28909923/1491895 for a more general approach to building queries dynamically.
If you know a value that will not be used, such as 0 (or -1); you can just check for that:
WHERE c.call_state_id = $state
AND ($user_id = 0 OR follow_emp_id = $user_id OR follow_emp_id = 117)
ORDER BY $sort

MYSQL - How to update a value?

After selecting the record with the current KEY and PROGRAM, I want to update the 'log_info' field for that CURRENT record, inputting the date/time. However, I keep getting the 'die error' for the update code.
ERROR CODE:
"Warning: mysql_query() expects parameter 1 to be string"
(for the "$log_info = mysql_query($query,"UPDATE..." code)
Snippet
$query = mysql_query("SELECT *
FROM `product_keys`
WHERE `serial_key` = '".$key."'
AND `program` = '".$prog."' LIMIT 1") or die('error selecting');
// UPDATE 'log_info' value to the latest date and time
$time = time();
$log_time = date('Y-m-d g:i:sa',$time);
$log_info = mysql_query($query,"UPDATE 'product_keys'
SET 'log_info' = '".$log_time."'
WHERE `serial_key` = '".$key."'
AND `program` = '".$prog."' LIMIT 1") or die('log error');
Try this line:
$log_info = mysql_query($query,"UPDATE `product_keys` SET `log_info` = '".$log_time."' WHERE `serial_key` = '".$key."' AND `program` = '".$prog."' LIMIT 1") or die(mysql_error());
Using mysql_error() you can catch specific error of query.
See The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
You have ' around the column name 'log_info' and the table name 'product_keys'. That should be backticks.
update query will be like this
$log_info = mysql_query("UPDATE 'product_keys'
SET 'log_info' = '".$log_time."'
WHERE `serial_key` = '".$key."'
AND `program` = '".$prog."'
LIMIT 1") or die('log error');
because mysql_query function accept 2 Parameters, first Parameter should be sql query , second Parameter is optional that is MySQL connection.

Why is my SQL UPDATE statement not working in a loop?

The update statement in example is not working all the time even though the where clause is true. The database is MYSQL innodb. Would that cause some sort of locking ?? This is so weird.
<?php
$query = 'SELECT id FROM TABLE1';
$result = db_query($query);
while($row = db_fetch_array($result)) {
//do some processing
db_query('UPDATE {TABLE1} SET updated = "1" WHERE id = "%s"',$row['id']);
}
?>
The syntax is wrong - MySQL doesn't use curly brackets:
db_query('UPDATE `TABLE1` SET updated = "1" WHERE id = "%s"',$row['id']);