How to create a stored procedure in mysql - php

I have created the following stored procedure in mysql...
DELIMITER //
CREATE PROCEDURE GetMember(IN in_memberID int)
BEGIN
SELECT *
FROM Members
WHERE MemberID = in_memberID;
END//
$result = mysql_query("CALL GetMember(".$memberID.")") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo $row['Name'] . "</br>";
}
But when I call it from php it returns all records in the Members table, what am I doing wrong?
EDIT:
When I try to call the query within phpmyadmin I get this error
CALL GetMember(1);
#1312 - PROCEDURE myDb.GetMember can't return a result set in the given context

what version of PHP?
PHP 5.2.3 and PHP 5.2.4 have a bug with procedures:
https://bugs.php.net/bug.php?id=42548

use Database_Name;
DELIMETER $$
DROP PROCEDURE IF EXISTS Proc$$
CREATE PROCEDURE Proc()
BEGIN
DECLARE x INT;
SET x = 1;
WHILE x <= 110000 DO
INSERT INTO Table_Name (word, mean) VALUES ('a', 'a mean');
SET x = x + 1;
END WHILE;
END$$
DELIMITER ;

Related

get output value from mysql stored-procedure to php?

how i can get output value from stored procedure to php variable ..
example Stored procedure :
DROP PROCEDURE `test`; CREATE DEFINER=`mydbinstance`#`%` PROCEDURE `test`(OUT `max` INT(11)) NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER
BEGIN
SELECT MAX(slno)+1 INTO #max FROM table1;
SELECT #max;
END
php:
$stmt=mysqli_query($conn,"CALL test();");
You should be able to get the result(s) of a stored procedure the same way you would any other query:
$stmt = mysqli_query($conn,"CALL test();");
if ($row = mysqli_fetch_row($stmt)) {
$value = $row[0];
}

Call count result from stored procedure MySQL in PHP

I'm trying to get a value from a stored procedure in php but I can't do it.
My stored procedure:
DROP PROCEDURE IF EXISTS sp_table;
DELIMITER $$
CREATE PROCEDURE sp_table()
BEGIN
SELECT COUNT(*) FROM table;
END$$
DELIMITER ;
My PHP code:
$recordSet_table = $conn->query("CALL sp_table()");
print_r($recordSet_table)."<br><br>";
Please try following code.
$sql = mysqli_query($connectionVariable,"CALL sp_table(#count)");
$result = mysqli_fetch_array($sql);

Unable to create mysql trigger from php

How do I use php and mysql to create the following trigger for each table created.
I want to convert the following code to be used as a sql query from php.
DELIMITER $$
CREATE TRIGGER trigger1
BEFORE INSERT
ON table1
FOR EACH ROW
BEGIN
SELECT COUNT(*) INTO #cnt FROM table1;
IF #cnt >= 25 THEN
CALL sth();
END IF;
END
$$
DELIMITER ;
I tried to use the following but it does not work.
if ($method == "T")// method is activated if $_GET["method'] = T method is set to T in script to allways return true
{
$sql = "CREATE TRIGGER TRIGGER1
BEFORE INSERT
ON 49a64d6512
FOR EACH ROW
BEGIN
SELECT COUNT(*) INTO #cnt FROM 49a64d6512;
IF #cnt >=25 THEN
CALL sth();
END IF;
END";
echo '1';
}
Is what i am trying to do even possible?

Mysql Stored Procedure and Php

I have created this procedure:
DROP PROCEDURE IF EXISTS add_com;
DELIMITER //
CREATE PROCEDURE add_com (cd CHAR(16), n VARCHAR(20), t VARCHAR(20), i VARCHAR(20))
BEGIN
DECLARE num INT;
DECLARE msg varchar(20);
set #num=select COUNT(*) from commercianti where codice_fiscale=cd;
IF num==0 THEN
insert into commercianti values (cd,n,i,t);
set #msg="Commerciante inserito";
ELSE
insert into errors values (1);
set #msg="Commerciante presente";
END IF;
return #msg;
END; //
then in a PHP page I execute this code:
<?php
$cd=$_POST['codice_fiscale'];
$n=$_POST['nominativo'];
$t=$_POST['telefono'];
$i=$_POST['indirizzo'];
if(!$cd||!$n||!$t||!$i)
echo "No data";
else{
require dirname(__FILE__) . '/' . 'dbconfig.php';
$mysqli = new MySQLI(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$result = $mysqli->query("CALL add_com('$cd','$n','$t','$i')");
echo $result;
}
?>
But the value of $result is undefined and seems the procedure doesn't work or isn't called.
If you want to get data back from a routine, you should be using a function, rather than a procedure. They have nearly the same syntax, but most notably is the RETURNS type section of the declaration. Take a look at the official documentation:
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
Note that the SECOND create block there is the one regarding functions. The first deals with procedures.
So I think that would look something like this:
DROP FUNCTION IF EXISTS add_com;
DELIMITER //
CREATE FUNCTION add_com (cd CHAR(16), n VARCHAR(20), t VARCHAR(20), i VARCHAR(20))
RETURNS VARCHAR(20)
BEGIN
DECLARE num INT;
DECLARE msg varchar(20);
SELECT COUNT(*) INTO #num FROM commercianti WHERE codice_fiscale = cd;
IF num==0 THEN
insert into commercianti values (cd,n,i,t);
set #msg="Commerciante inserito";
ELSE
insert into errors values (1);
set #msg="Commerciante presente";
END IF;
return #msg;
END; //
I'm not 100% sure about the RETURNS VARCHAR(20). That might have to be simply RETURNS VARCHAR.
Try changing
IF num==0 THEN
to
IF num=0 THEN

how to update multiple columns from a trigger MySQL

hi i am using mysql trigger to update a table on another table's insertion
this trigger works fine
CREATE TRIGGER `update_pupil_subject` AFTER INSERT ON `pupil_marks`
FOR EACH ROW
BEGIN
UPDATE pupil_subjects SET NumberOfStudens = NumberOfStudens + 1 WHERE NEW.SubjectID = SubjectID;
END$$
but this gives an error
CREATE TRIGGER `update_pupil_subject` AFTER INSERT ON `pupil_marks`
FOR EACH ROW
BEGIN
UPDATE pupil_subjects SET NumberOfStudens = NumberOfStudens + 1 , AverageMarks = (SELECT AVG(Marks) FROM pupil_marks WHERE NEW.StudentID = StudentID ) WHERE NEW.SubjectID = SubjectID;
END$$
how to write this correctly , please help . thanks in advance .
Apparently there were problems when sub-queries were used:
Can you try splitting the SQL statement:
DELIMITER $$
CREATE TRIGGER `update_pupil_subject`
AFTER INSERT
ON `pupil_marks`
FOR EACH ROW
BEGIN
DECLARE avg_marks float;
SELECT AVG(Marks)
INTO avg_marks
FROM pupil_marks
WHERE NEW.SubjectID = SubjectID;
UPDATE pupil_subjects
SET NumberOfStudens = NumberOfStudens + 1, AverageMarks = avg_marks
WHERE NEW.SubjectID = SubjectID;
END
$$
Edit: Use
SHOW TRIGGERS WHERE `table` = 'pupil_marks';
to get all triggers defined on pupil_marks. You can't have multiple triggers on an event as all actions can be covered in single trigger.
NOTE: I think AVG(Marks) is for a given subject, so modified trigger definition accordingly.
declare a variable inside the trigger and assign it with the subquery
declare avg_mark integer default 0;
set avg_mark := (SELECT AVG(Marks) FROM pupil_marks WHERE NEW.StudentID = StudentID);
then use the variable "avg_mark" in your update statement...
it may work...
if not then check the delimiter just below phpmyadmin sql box . It should be "$$"

Categories