Home  >  Article  >  Backend Development  >  How to call stored function in php? Call a stored procedure? What's the trigger?

How to call stored function in php? Call a stored procedure? What's the trigger?

慕斯
慕斯Original
2021-06-28 17:28:111788browse

We have learned so much about PHP, but I don’t know how to call stored functions in PHP? Call a stored procedure? What's the trigger? Have you fully mastered it? If not, then follow this article to continue learning

Related recommendations:What are the usages of is, between, in and other operators in PHP?

Instructions for using stored functions or stored procedures in php

//Calling stored functions:

$v1= $_POST["a];sv2 = $_POST["b];
$sq1 = "insert into tab1 (id, f2, f3)values ( null,nowO. fumnc1(Sv1 , $v2) )":$result= mysql_query($sql);

//Call stored procedure:

$v1 = $_POST[ "usemame"];$v2=$_POST["pass"];sv3= $_POST["age"];
$sql ="callinsert_user(Sv1,$v2,$v3 ); ";llinsert_user)是一个存储过程,带3个参数,会将该3个参数数据写入<insert〉某个表中。
Sresult = mysql_queryO;

Another example of using a stored procedure to return a result set

$sql m"call Get_User_iInfo( sid )“;//Get_User_info)是一个存储过程,其中会返回某个指定id 的用户信息$result = mysql_query(ssql);

What you get here is the "result set "

Trigger (trigger)

Meaning:

Trigger is also a predefined programming code 〈Same as stored procedures and stored functions〉, and has a name. But: it cannot be called, but when an event (add, delete, change) occurs in a certain table, it will be automatically "triggered" and called.

Definition form:

create trigger触发器名︰触发时机触发事件on表名foreach rowasbegin
//这里,才是编程的位置,也就是触发器的内部语句end3

Instructions:

1, there are only 2 trigger timings: before《before...), after》 ﹔2. There are only 3 trigger events: insert, update, delete

2. There are only 3 trigger events: insert, update, delete

3, that is, the meaning of the trigger is: Before (or after) insert (or update, or delete) on a certain table, the code (statement) written in it will be executed; that is, there are only 6 situations in each table that may call the trigger,

4. Usually, triggers are used when adding, deleting, or modifying a table and need to do another thing at the same time; 5. Inside the trigger, there are two keywords representing some kind of Specific meaning, you can use to obtain data:

new:

It represents the "new row" data when the insert update is currently being executed; through it , you can get the value of any field of this new row of data, in the form:

set@v1 = new.id;

//Get the value of the id field of the newly inserted or updated row (provided that the id exists)

set@v2 = new.age;//同上,

old:

It represents the "old row" data when the delete is currently being executed. Through it, you can get the value of any field of this old row data in the form:

set @v1 = old.id:I

Get the value of the id field of the newly inserted or updated row (provided that the id exists)

set @v2 = old.age;//同上;

#Delete all the data in tab_int_max first:

delete from tab_int_max1;

#Get tab_int The maximum value of the f1 field in is stored in the variable @maxfselect max(f1) into @maxf1 from tab_int;

#Then, based on the maximum value of the f1 field obtained, as a condition, 3 fields are taken out Value:

select f2 into @v2 from tab_int where f1 = @maxf1;
select f3 into @v3 from tab_int _ where f1 = @maxf1;#然后,将@maxf1,@v2,@v3插入到表tab_int_max1
insert into tab_int_max1 (f1,f2,f3)values( @maxf1,@v2,@v3);end;
#再建一个触发器,在表tab_int进行insert之前,将该行数据#也同时插入到一个跟其类似结果的表(tab_int_some)中
:CREATE TABLE tab_int_some (
id int(11) DEFAULT NULL,age tinyint(4) DEFAULT NULL);
create trigger copy_data before insert on tab_int for each rowbegin
set @v1 = new.f1;
#获得新行的字段f1的值;
set @v2 = new.f2;#获得新行的字段f2的值;
insert into tab_int_some(id,age)values(@v1,@v2);end;

Recommended learning: php video tutorial

The above is the detailed content of How to call stored function in php? Call a stored procedure? What's the trigger?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn