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?
I have table status and status_stage . After insert in status table, I want to add it to the status_stage table. I have this procedure:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `addStatus`(status VARCHAR(45), stage int (8))
BEGIN
DECLARE status_id INT DEFAULT NULL;
INSERT INTO status (status)
VALUES (status);
SELECT id INTO status_id FROM status WHERE status = status;
INSERT INTO stage_status (stage,status)
VALUES (stage,id);
END
And I call that function with this:
$result= $this->db->query("call addStatus('$status', $stage)");
return ($result->num_rows()>0);
When I tried it it gives me this Error:
Result consisted of more than one row
Your SELECT ... INTO statement got more than one result because of your WHERE clause with status = status, this equals forever
you need to change the param name to distinguish it from the table column name (eg. status_param)
I'm trying to log changes of a table into another table. But my trigger is not working. Need help:
// Mysql code below
create trigger upd_stu_info
before update
on kisalg_student
for each row
begin
IF OLD.stu_name != NEW.stu_name
THEN
INSERT INTO `kisalg_logactions`
(adm_no, change_type,change_head, old_value,new_value,user,date_time,session)
VALUES
(NEW.adm_no, 'stu_info','Change In Student Name ', OLD.stu_name, NEW.stu_name, NEW.user, NEW.date_time, NEW.session);
END IF;
IF OLD.fat_name != NEW.fat_name
THEN
INSERT INTO `kisalg_logactions`
(adm_no, change_type, change_head, old_value, new_value, user, date_time, session)
VALUES
(NEW.adm_no, 'stu_info','Change In Father Name ', OLD.fat_name, NEW.fat_name, NEW.user, NEW.date_time, NEW.session);
END IF;
end;
I just tried the trigger on mysql and using the following I did not get any syntax error
delimiter //
create trigger upd_stu_info
before update
on kisalg_student
for each row
begin
IF OLD.stu_name != NEW.stu_name
THEN
INSERT INTO `kisalg_logactions`
(adm_no, change_type,change_head, old_value,new_value,user,date_time,session)
VALUES
(NEW.adm_no, 'stu_info','Change In Student Name ', OLD.stu_name, NEW.stu_name, NEW.user, NEW.date_time, NEW.session);
END IF;
IF OLD.fat_name != NEW.fat_name
THEN
INSERT INTO `kisalg_logactions`
(adm_no, change_type, change_head, old_value, new_value, `user`, date_time, session)
VALUES
(NEW.adm_no, 'stu_info','Change In Father Name ', OLD.fat_name, NEW.fat_name, NEW.`user`, NEW.date_time, NEW.session);
END IF;
end; //
I have the error : Result consisted of more than one row but i can't find out it.
The entry id_user has only one record in its table.
The procedure:
CREATE DEFINER=`root`#`localhost` PROCEDURE `delete_user_deep`(id_user int(11))
BEGIN
declare nome_loc varchar(45);
declare cognome_loc varchar(45);
select nome,cognome
into nome_loc,cognome_loc
from user
where id_user=id_user;
delete from Radius.radcheck where username in (
select mac_address from machine where id_user =id_user);
delete from Radius.radreply where username in (
select mac_address from machine where id_user =id_user);
delete from machine_ip where id_machine in (
select id_machine from machine where id_user=id_user);
delete from machine where id_user = id_user;
delete from document where id_user=id_user;
delete from user where id_user=id_user;
insert into log_generic values(
NULL,
'USER',
'Delete User Deep',
(select concat ('User: ',id_user,' Name: ',cognome_loc,' Prename: ',nome_loc)),
now());
END
The problem is in this statement:
select nome,cognome
into nome_loc,cognome_loc
from user
where id_user=id_user;
This will return an error if there is more than one row. The error is documented here.
This is caused because the parameter name is the same as the column name. The id_user is referring to the column on both sides of the comparison.
The query should look like:
select nome, cognome
into nome_loc, cognome_loc
from user
where id_user = param_id_user;
Always prefix parameter and variable names with something to distinguish them from column names.
I have written a stored procedure that takes comma separated value as input and another value. I traverse values from comma separated value and then run a query for each value. now I need to return result so I thought to store results in temporary table and then selected values from that temporary table.But it is taking too long.
for query with 163 comma separated values it is taking 7 seconds.
for query with 295 comma separated values it is taking 12 seconds.
Here is procedure:-
DELIMITER $
create procedure check_fbid_exists(IN myArrayOfValue TEXT, IN leaderID INT(11) )
BEGIN
DECLARE status TINYINT(1);
DECLARE value INT(11);
DECLARE pos int(11);
CREATE TEMPORARY TABLE fbid_exists_result (userID int(11), status tinyint(1));
WHILE (CHAR_LENGTH(myArrayOfValue) > 0)
DO
SET pos=LOCATE( ',', myArrayOfValue);
IF pos>0 THEN
SET value = LEFT( myArrayOfValue,pos-1 );
SET myArrayOfValue= SUBSTRING( myArrayOfValue,pos+1 );
ELSE
SET value = myArrayOfValue;
SET myArrayOfValue='';
END IF;
SELECT EXISTS(SELECT 1 FROM users_followings WHERE UserID=value and LeaderUserID=leaderID LIMIT 1) INTO status;
insert into fbid_exists_result VALUES(value,status);
END WHILE;
SELECT * FROM fbid_exists_result ;
DROP TEMPORARY TABLE IF EXISTS fbid_exists_result ;
END$