MySQL Stored Procedure erro - php

I am getting this error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lstart, lend; END IF; ELSE IF lstart = -1 THEN select count(*) a' at line 8"
on this stored procedure creation on server but in my localhost phpmyadmin it is creating without any error?
DELIMITER
create procedure getallmedia(IN cat int(2), IN asc_desc varchar(5), IN lstart int(11), IN lend int(11))
BEGIN
IF cat = -1 THEN
IF asc_desc = 'asc' THEN
IF lstart = -1 THEN
select count(*) as result from bc_media where id <> -1;
ELSE
select * from bc_media where id <> -1 limit lstart, lend;
END IF;
ELSE
IF lstart = -1 THEN
select count(*) as result from bc_media where category = cat;
ELSE
select * from bc_media where category = cat order by id desc limit lstart, lend;
END IF;
END IF;
ELSE
IF asc_desc = 'asc' THEN
IF lstart = -1 THEN
select count(*) as result from bc_media where category = cat;
ELSE
select * from bc_media where category = cat limit lstart, lend;
END IF;
ELSE
IF lstart = -1 THEN
select count(*) as result from bc_media where category = cat;
ELSE
select * from bc_media where category = cat order by id desc limit lstart, lend;
END IF;
END IF;
END IF;
END
DELIMITER ;
Help me out please :(

I'm not sure, but, at the first DELIMITER try to put $$
And after the last END put $$

Related

My mySQL stored add update delete procedure replaces the last entry in the table whenever I add a new entry

Experienced with Oracle and Microsoft SQL, but super new to mySQL and have never encountered this problem before. Any thoughts? Code below
CREATE PROCEDURE spAddUpdateDelete_Product (IN productId INT, IN title VARCHAR(200), IN description VARCHAR(200), IN price FLOAT, IN categoryId INT, IN toDelete BIT)
BEGIN
IF productId = 0 THEN -- Add
IF EXISTS(SELECT productId FROM Product WHERE title = title AND description = description AND price=price and categoryId = categoryId) OR NOT EXISTS(SELECT categoryId FROM Category WHERE categoryid=categoryid) THEN
SELECT -1;
ELSE
INSERT INTO Product (productTitle, description, price, categoryId) VALUES (title, description, price, categoryId);
SELECT LAST_INSERT_ID();
END IF;
END IF;
IF toDelete = 1 THEN -- Delete
IF EXISTS(SELECT reviewId FROM Review WHERE productId=productId) THEN
UPDATE Product SET toDelete = 1 WHERE productId = productId;
SELECT 0;
ELSE
DELETE FROM Product WHERE productId = productId;
SELECT 0;
END IF;
END IF;
IF EXISTS(SELECT productId FROM Product WHERE productId=productId) THEN -- Update
UPDATE Product SET productTitle=title, description=description, price=price, categoryId=categoryId;
SELECT productId;
ELSE
SELECT -1;
END IF;
END $$
Figured this one out. I didn't realize using SELECT doesn't work like using a return in a function. Updated code below.
CREATE PROCEDURE spAddUpdateDelete_Product (IN productId INT, IN title VARCHAR(200), IN description VARCHAR(200), IN price FLOAT, IN categoryId INT, IN toDelete BIT)
BEGIN
IF toDelete = 0 THEN -- Add/Update
IF NOT EXISTS(SELECT categoryId FROM Category c WHERE c.categoryid=categoryid) THEN
SELECT -1;
ELSE
IF productId = 0 THEN
INSERT INTO Product (productTitle, description, price, categoryId) VALUES (title, description, price, categoryId);
SELECT LAST_INSERT_ID() AS productId;
ELSE
UPDATE Product SET Product.productTitle = title, Product.description = description, Product.price = price, Product.categoryId = categoryId WHERE Product.productId = productId;
SELECT productId;
END IF;
END IF;
END IF;
IF toDelete = 1 THEN -- Delete
IF EXISTS (SELECT productId FROM product WHERE Product.productId = productId) THEN
IF EXISTS(SELECT reviewId FROM Review WHERE Review.productId=productId) THEN
UPDATE Product SET toDelete = 1 WHERE Product.productId = productId;
SELECT 0;
ELSE
DELETE FROM Product WHERE Product.productId = productId;
SELECT 0;
END IF;
ELSE
SELECT -1;
END IF;
END IF;
END $$

mysql select with if condition

I have a function in Mysql that is called like this:
DB::raw("count_adults(rooms.id) as adults"),
count_adults is this:
BEGIN
DECLARE adults INT;
SELECT
count(*) INTO adults
FROM
clients
WHERE
clients.room_id = r_id
and (age >= 18
or age = 0);
RETURN adults;
END
However I want to if this query returns no results(0) do another select or function, like this:
SELECT count(*) INTO adults FROM client_room, clients
WHERE client_id = clients.id and client_room.room_id = r_id and (age >18 or age = 0)
Can I create a if condition? IN the php code? Or in the query? Something like this:
IF EXISTS (SELECT 1 FROM clients WHERE clients.room_id = r_id)
BEGIN
DECLARE adults INT;
SELECT
count(*) INTO adults
FROM
clients
WHERE
clients.room_id = r_id
and (age >= 18
or age = 0);
RETURN adults;
END
ELSE
BEGIN
DECLARE adults INT;
SELECT count(*) INTO adults
FROM
client_room, clients
WHERE
client_id = clients.id and client_room.room_id = r_id
and (age >18
or age = 0);
END
I have tried this and it seems to work:
BEGIN
DECLARE adults INT;
SELECT COUNT(*) INTO adults
FROM clients
WHERE clients.room_id = r_id
and (age >= 18 or age = 0);
RETURN IF( adults>0, adults, (SELECT count(*) FROM client_room, clients
WHERE client_id = clients.id
and client_room.room_id = r_id)
);
END
You could use CASE in the query, e.g.:
CASE
WHEN function1() > 0
THEN function1()
WHEN function2() > 0
THEN function2()
ELSE
0
END
That will evaluate to the value returned from function1() if it is larger than 0. If it's not but the value returned by function2() is larger then 0, then this is taken. Otherwise it's 0.
Instead of function1(), function2() etc. a column, subquery (that is, if it returns a scalar), literal, etc. can be used. And of course it can be changed to more or less conditions.

limit maximum input to the database php mysql?

how can i limit the maximum number of rows in mysql. For example, if the number of grade 4, section 1 students is 30 or over, the user cannot add another student.
first count the rows of the given conditions
$res = mysql_query("SELECT count(*) as cnt FROM `your_table` WHERE grade='4' AND section='1'");
$row = mysql_fetch_assoc($res);
if($row['cnt']<30)
{
// insert student
}
else
{
// not to insert student
}
DELIMITER $$
CREATE TRIGGER trigger1
BEFORE INSERT
ON table1
FOR EACH ROW
BEGIN
SELECT COUNT(*) INTO #cnt FROM table1;
IF #cnt > 30 THEN
/**raise an error**/
END IF;
END
$$
DELIMITER ;

PHP Oracle Pagination Cant Load Data In Next Page

This question is continue from my last question.
I have jQuery Table Pagination using ORACLE database.
The code like this :
$cur_page = $page;
$page -= 1;
$per_page = 14;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
$query_pag_data = "
SELECT * FROM
(
SELECT x.*, ROWNUM as r FROM
(
SELECT P.PRODUCTION_STATUS, P.DATE_ADDED, P.FORM_NO, P.QTY_PLAN, ROWNUM, M.MODEL_NO, M.MODEL_NAME
FROM SEIAPPS_PRODUCTION_STATUS P, SEIAPPS_MODEL M
WHERE P.MODEL_NO = M.MODEL_NO
ORDER BY P.DATE_ADDED DESC, P.TIME
) x
)
WHERE r >= $start AND r <= $per_page
";
I limit per page is 14 row.
The result for first page is OK, no problem. But when I try to open next page, it's not load the data. Whereas I have more 20 row data in table.
Anyone please help.
Thanks.
Try like this,
SELECT * FROM
(
SELECT x.* FROM
(
SELECT p.production_status, p.date_added, p.form_no, p.qty_plan, ROWNUM r, m.model_no, m.model_name
FROM seiapps_production_status p, seiapps_model m
WHERE p.model_no = m.model_no
ORDER BY p.date_added DESC, p.TIME
) x
)
WHERE r >= $start AND r <= $per_page
;
If you want to get the result with page number and line per page, try with the below procedure.
CREATE OR REPLACE PROCEDURE pagination_proc(
i_page_num IN NUMBER,
i_lines_per_page IN NUMBER,
o_result OUT sys_refcursor)
AS
l_max_row NUMBER(4) := i_page_num * i_lines_per_page;
l_rec_count NUMBER(4);
BEGIN
IF i_page_num = 1 THEN
SELECT count(*)
INTO l_rec_count
FROM seiapps_production_status p, seiapps_model m
WHERE p.model_no = m.model_no;
END IF;
IF l_rec_count > 0 OR i_page_num >1 THEN
OPEN o_result FOR
SELECT production_status, date_added, form_no, qty_plan, model_no, model_name
FROM
(
SELECT production_status, date_added, form_no, qty_plan, model_no, model_name, ROWNUM rn
FROM
(
SELECT p.production_status, p.date_added, p.form_no, p.qty_plan, m.model_no, m.model_name
FROM seiapps_production_status p, seiapps_model m
WHERE p.model_no = m.model_no
)
WHERE ROWNUM <= l_max_row
)
WHERE rn > l_max_row - i_lines_per_page;
END IF;
END;

How do I rename duplicates in MySQL using PHP or just a MySQL

trying to rename duplicates in MySQL database so far using that code but this only adding 1 at the end of name. So if I have
UPDATE phpfox_photo n
JOIN (SELECT title_url, MIN(photo_id) min_id FROM phpfox_photo GROUP BY title_url HAVING COUNT(*) > 1) d
ON n.title_url = d.title_url AND n.photo_id <> d.min_id
SET n.title_url = CONCAT(n.title_url, '1');
Anna
Anna
Anna
Result is
Anna
Anna1
Anna11
When I got 200 Annas result is Anna1111111111111111111111111111111111111111111....etc
how do I do it to rename in the following inc
Anna
Anna1
Anna2
if i didn't miss something you can make a stored procedure that iterates throw your rows using cursors to do that as following:
DECLARE counter INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
DECLARE offset INT;
DECLARE title_urlvalue VARCHAR(50);
DECLARE no_more_rows BOOLEAN;
DECLARE ucur CURSOR FOR
SELECT
UPDATE phpfox_photo n
JOIN (SELECT title_url, MIN(photo_id) min_id
FROM phpfox_photo GROUP BY title_url HAVING COUNT(*) > 1) d
ON n.title_url = d.title_url AND n.photo_id <> d.min_id;
SET offset = 1;
SET no_more_rows = TRUE;
select FOUND_ROWS() into num_rows;
OPEN ucur;
uloop: LOOP
FETCH ucur
if counter >= num_rows then
no_more_rows = False;
endif
INTO title_urlvalue;
IF no_more_rows THEN
CLOSE ucur;
LEAVE uloop;
END IF;
update title_urlvalue = Concat(title_urlvalue,offset);
SET offset = offset + 1;
SET counter = counter + 1;
END LOOP uloop;
close ucur;
With User-Defined Variables
SET #counter:=0;
SET #title_url:='';
UPDATE phpfox_photo n
JOIN (SELECT title_url, MIN(photo_id) min_id
FROM phpfox_photo
GROUP BY title_url
HAVING COUNT(*) > 1) d
ON n.title_url = d.title_url AND n.photo_id <> d.min_id
SET n.title_url = IF(n.title_url <> #title_url, CONCAT(#title_url:=n.title_url, #counter:=1), CONCAT(n.title_url, #counter:=#counter+1));
Maybe you can use modulo to produce numbering, like this (SQLite example, but should be similar in mysql):
SELECT *, (rowid % (SELECT COUNT(*) FROM table as t WHERE t.name = table.name ) ) FROM table ORDER BY name
All you need is to translate rowid and modulo function, both availible in mysql.
Then you can CONCAT results as you desire.
UPDATE phpfox_photo n
JOIN
(SELECT title_url,
MIN(photo_id) min_id
FROM phpfox_photo
GROUP BY title_url
HAVING COUNT(*) > 1
)
d
ON n.title_url = d.title_url
AND n.photo_id <> d.min_id
SET n.title_url =
CASE
WHEN <last char is int>
THEN <replace last char with incremented last char>
ELSE <string + 1>
END

Categories