Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8321591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:04:20+00:00 2026-06-08T23:04:20+00:00

I’m getting an error when trying to declare a new stored function in MySQL

  • 0

I’m getting an error when trying to declare a new stored function in MySQL (Server version: 5.5.13)

Basically, I have a large table which classifies strings depending on how they start. My function takes a string (from user input) and then tells you the classification of that string by searching the database for the classification. It’s a bit like a LIKE query, except in reverse as it’s the user input that contains the full string and the database contains the string being searched for. Hope that makes sense!

The concept and logic behind it work fine as I’ve written/developed this in PHP and it works beautifully, however when trying to translate this into a stored function I’m getting an error from MySQL. The code of the function is:

delimiter $

DROP FUNCTION IF EXISTS get_string_class$
CREATE FUNCTION get_string_class(mystring VARCHAR(15))

RETURNS VARCHAR(15)
READS SQL DATA
BEGIN

DECLARE i INT;
SET i = 2;

DECLARE mystringlength INT;
SET mystringlength = LENGTH(mystring);

DECLARE segment VARCHAR(15);
DECLARE String_Class VARCHAR(15);
SET String_Class = NULL;

WHILE i <= mystringlength DO

   SET segment = LEFT(mystring, i);

   SET String_Class = (SELECT String_Class FROM string_class_list WHERE String_Begins = segment);

   IF SELECT FOUND_ROWS() = 1 THEN
      RETURN String_Class
   END IF;

   i = i + 1;

END WHILE;

RETURN String_Class;

END$
delimiter ;

I’m 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 
'DECLARE mystringlength INT; SET mystringlength = LENGTH(mystring);
DECLARE segm' at line 10 

I’ve done lots of playing around to trying and work out where I’m going wrong. I’ve even stripped out the loop altogether to test it, but still get the same error. Does anyone know what I’ve done wrong where declaring that INT variable? It’s probably something incredibly basic…!

Thanks very much in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:04:23+00:00Added an answer on June 8, 2026 at 11:04 pm

    There were some syntax errors –

    One of them is – all declarations must be at the begining of the BEGIN…END clause.

    DELIMITER $
    
    DROP FUNCTION IF EXISTS get_string_class$
    CREATE FUNCTION get_string_class(mystring VARCHAR(15))
    
    RETURNS VARCHAR(15)
    READS SQL DATA
    BEGIN
    
    DECLARE i INT;
    DECLARE segment VARCHAR(15);
    DECLARE String_Class VARCHAR(15);
    DECLARE mystringlength INT;
    
    SET i = 2;
    SET mystringlength = LENGTH(mystring);
    SET String_Class = NULL;
    
    WHILE i <= mystringlength DO
    
       SET segment = LEFT(mystring, i);
    
       SET String_Class = (SELECT String_Class FROM string_class_list WHERE String_Begins = segment);
    
       IF (SELECT FOUND_ROWS()) = 1 THEN
          RETURN String_Class;
       END IF;
    
       SET i = i + 1;
    
    END WHILE;
    
    RETURN String_Class;
    
    END$
    DELIMITER ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.