Capturing MySQL Statement and inserting it into another db via trigger - mysql

I want to be able to keep a log of every sql statment that edits a specific table, and I think the best way would be with a trigger. However, I don't know how to get the statement that triggers the trigger.
For example if I run:
$sql = "INSERT INTO foo (col_1) VALUES ('bar');"
I want to be able to craft an insert statement like:
$sql = "INSERT INTO log (statements) VALUES (INSERT INTO foo (col_1) VALUES ('bar')'
Is this possible with a trigger? I know I can do this easily with php, but I figured that a trigger would have less overhead.

You can't get the statements, only their results / alterations. Consider using a database abstraction- or decorator-interface to capture those queries.

Related

Combining 2 Queries with OR operator

I'm trying to insert something to a table and also delete at the same time so my query is like this
$query = mysqli_query($connect,"SELECT * FROM inventory_item WHERE status = 'Unserviceable' OR DELETE * FROM inventory_item WHERE status = 'Available")
or die ("Error: Could not fetch rows!");
$count = 0;
I wanted to insert datas with Unserviceable status and at the same time delete datas with Available status but its not working.
I'm not really familiar with queries and just starting out.
This is not valid SQL syntax.
If you want to issue two queries, one to INSERT and one to DELETE, then you can send them as two separate calls to mysqli_query(). There appears to be an alternate function mysqli_multi_query() that allows multiple statements to be included which you can read about here.
Finally, if you want the two separate queries to execute as a single unit (that is, if one of them fails then neither is executed) then you should research the subject of database transactions, which allow you to execute multiple queries and commit or roll back the entire set of queries as a unit.

MYsql - Get insert id - sql

I want to execute a query that insert the insert id to one of the colums
And I want to know how to get the insert id
I read this answer Get inserted id from mysql insert procedure
but its talk about how to get the insert id after the query, And I want to get it when I do the query
I try to do:
$stmt = $db->prepare("INSERT INTO `files` (`name`, `fname`) VALUES (?, LAST_INSERT_ID() + ?)") or die($db->error);
$stmt->bind_param('ss', $name, $uniqid);
But its dosent work, I always get 56, also the contcat dosent work, what I need to do?
It is simply easier and better to save it after the insert. It is also highly unlikely you will get the correct id before the actual insert happens.
The most reliable way would be to use transactions.
Start a transaction
Insert the row
Update the insert id
Commit the transaction
The + is not a string concatenation operator in MySQL. Use CONCAT(string1, string2, ...).
In the wire protocol, mysql returns the same value as LAST_INSERT_ID() to the client after each query, unsolicited, and most libraries provide a way to access the most recently returned value. You didn't mention, but it looks like you're using mysqli in php, so you're looking for this:
$last_id = $db->insert_id;
It may look as if this is going to send a second query to ask the database for the id, but it isn't. It's returning the previously stored value.
http://php.net/manual/en/mysqli.insert-id.php

I need to find out who last updated a mysql database

I can get a last update time from TABLES in information_schema. Can I get a USER who updated the database or a table?
As Amadan mentioned, I'm pretty sure there isn't a way to do this unless you record it yourself. However, this is a pretty straightforward thing to do: Whenever you perform an UPDATE query, also log in a separate table the user (as well as any other relevant information) that you want to record via an additional MySQL query. Something like this (written in PHP as you didn't specify a language, but the MySQL can be exported anywhere) will work:
// The update query
$stmt = $db->prepare("UPDATE table SET `col` = ? WHERE `col` = ?");
$stmt->execute(array($var1, $var2));
// Something in table has just been updated; record user's id and time of update
$stmt = $db->prepare("INSERT INTO log (userid, `time`) VALUES (?, NOW())");
$stmt->execute(array($userid));

UPDATE table one... and INSERT into table two

When I try to UPDATE and INSERT into two different tables on the same user action, it has an error where it only completes the command of either the insert or the update depending on which line comes last.
Is there a way to combine these two ?
$sql = "INSERT INTO photos(user, gallery, filename, uploaddate)
VALUES ('$log_username','profile pictures','$db_file_name',now())";
$sql = "UPDATE users SET avatar='$db_file_name'
WHERE username='$log_username' LIMIT 1";
Have you thought about using a trigger on the photos table? You could setup a tigger to execute every time an insert occurs that would update the users table.
Here's a link to check out:
http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx
1) Execute the first query and then execute the second query.
2) use transaction if mysql if want want the successful execution of both the queries otherwise transaction control mechanism will revert the change.
And what error are you getting when you are firing the queries one by one

executing queries together to gain efficiency

Presently I am using prepared statements to execute a query or an update on the database. Though pre-compiled(and hence fast) I think that it would be even more efficient if could have such an arrangement:
Scenario:
Suppose I have to insert 100 rows in a database table. I use prepare statements so I prepare a statement and send it to the database for execution. So each time the query is of the form:
insert into user values(....);
Now consider this situation when I have a query of the form
insert into user values (...), (...), ....,(...);
By this we can minimize table access and execute query in one go.
Is there any way that we can do this using prepared statements or such an arrangement where we can instruct database that execute next 100 updates together. By the way I am presently working on mysql
INSERT statements that use VALUES syntax can insert multiple rows. To
do this, include multiple lists of column values, each enclosed within
parentheses and separated by commas. Example:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
Try something like
st = con.createStatement();
st.addBatch("INSERT INTO tbl_name VALUES(1,2,3)");
st.addBatch("INSERT INTO tbl_name VALUES(4,5,6)");
st.addBatch("INSERT INTO tbl_name VALUES(7,8,9)");
st.executeBatch();
Any reason not to do it as a bulk insert operation?
There's probably a better way of doing it, but I simply write a "file" to /dev/shm then reference in a LOAD DATA INFILE statement, thus never hitting disk on the client machine.