Help with SP and UDF?

I am trying to learn and use SP (stored procedure) and UDF (user-defined function) with MySQL and PHP. What is the difference between SP and UDF and what is the purpose?

How would a simple piece of code look like in PHP and SQL with an SP that updates, writes, or retrieves something from a table in a MySQL database? You need to understand and see the meaning of using SP and UDF.

Provide help! Thank!

+3
source share
1 answer

Saved Procedures

A stored procedure is MySQL code written and executed by MySQL.

Saved Function Example

CREATE FUNCTION AreWeThereYet(Location integer) RETURNS boolean
BEGIN
  Return 0;
END

Stored Procedure Example

CREATE PROCEDURE InsertRow(A integer)
BEGIN
  INSERT INTO table1 VALUES(A);
END

SLM

A UDF is C (++) or similar code compiled as .so (linux) or .dll (windows)

What are you inserting into MySQL using the command:

CREATE FUNCTION metaphon RETURNS STRING SONAME 'udf_example.so'; //linux
CREATE FUNCTION metaphon RETURNS STRING SONAME 'udf_example.dll'; //windows


UDF , .
SO
UDF , , , / ( / ..)


.:
http://dev.mysql.com/doc/refman/5.1/en/stored-routines.html

UDF .:
http://dev.mysql.com/doc/refman/5.1/en/udf-compiling.html

SO-
: MySQL? php?: PHP MySQL?
views sproc?: MySQL:
sproc : mysql
sproc: MySQL

php

-- normal select
$query = "SELECT * FROM table1";
-- call to stored proc
$query = "CALL InsertARow('1')";
-- use a stored function
$query = "SELECT AreWeThereYet('10')";
-- or
$query = "SELECT * FROM table1 WHERE AreWeThereYet(field1) = '1' ";

.

+4

All Articles