Pass PHP variable to MySQL trigger #nette

最后都变了- 提交于 2019-12-11 16:19:40

问题


I have trigger created in my database. I need to pass PHP variable to that trigger.

I tried executing something like this in PhpMyAdmin:

SET @myVariable = 123;
INSERT INTO table (foo) VALUES (bar);

Trigger successfully take @myVariable - So I tried it in Nette project, but I was not successfull.

First, Prepared Statement can execute only one command, so I am not able to call SET and INSERT together.

Then I found information that variable should be valid for whole connection, so I tried call $database->query('SET @myVariable = 123'); after connecting to database. Then I tried INSERT. But again, I was not successfull.

Anybody have any idea how to pass PHP variable to trigger?


回答1:


For set variable is suitable QUERY command. Never concatenate strings, always add the parameters with the question mark (SQL injection vulnerability)

$database->query('SET @myVariable = ?', 123);
$database->query('INSERT INTO table ?', ['foo' => bar]);



回答2:


To set a variable explicitly to a particular value, use a SET statement.

Syn:

SET @variable = value

Ex:

$db->query('SET @myVariable=?', '123' ); OR use mysql as mention above.

A set variable’s value persists until you assign it another value or until the end of your session.

So you can get your variable in Trigger,using select statement.

Syn:

SELECT @variable;

Ex:

In Trigger

IF (condition) THEN
  SET myVariable_t = (SELECT @myVariable);
   INSERT INTO table
     SET column_name = myVariable_t,          
END IF;



回答3:


For some reason, the problem was when I used variables with string. When I assigned integer value to variable, everything is working fine.



来源:https://stackoverflow.com/questions/50967147/pass-php-variable-to-mysql-trigger-nette

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!