Search code examples
mysqlphpmyadminmysql-error-1064

MySQL function return value from row in table


I am trying to return a value from a table. The table is a parameter table that will have it's value changed in the future. The table name is 'Parameter' and I'm trying to get the value from the value column where the term is = 'Base URL'

In PHP My Admin, I am creating a function with the following query and it's giving me an error. I can't seem to locate the error.

DECLARE @url VARCHAR;
SET @url = '';
SELECT p.value INTO @url FROM Parameters p WHERE p.Term = 'Base URL';
RETURN @url;

I am getting an error 1064 near "DECLARE @url VARCHAR(255); SET @url = ''; SELECT p.value INTO @url FROM Param"

Where is my error? I couldn't find a similar situation. I have used parameter tables in the past, but never created one or a function to go along with it. Is there a better way to do what I want?

Thanks!


Solution

  • I have tried to create similar function like this and remove this line make it works

        DECLARE @url VARCHAR;
    

    May be you have some confuses between variable with @ or not. This link may be useful for you MySQL: @variable vs. variable. What's the difference?

    One more thing, some people may have problem with delimiter. I get this work

        DROP FUNCTION IF EXISTS get_url;
    
        DELIMITER $$
        CREATE FUNCTION get_url()
        RETURNS varchar(255)
        BEGIN 
          DECLARE url varchar(255);
    
            SELECT p.value INTO url FROM Parameters p WHERE p.Term = 'Base URL';
    
          RETURN url;
        END$$
    
        DELIMITER ;