How to safely retrieve sql data using a stored function

conectionist

I'm trying to write a function in MySQL that gets the password for a username given as parameter, while avoiding SQL injection.

Here's my code:

CREATE FUNCTION get_password_for_user (p_username varchar(30))
    returns varchar(32)
BEGIN
    declare passwd varchar(32) default "";

   PREPARE stmt FROM 'select password into @passwd from users where name=?';
   SET @name = p_username;
   EXECUTE stmt USING @name;
   DEALLOCATE PREPARE stmt;

   return passwd;
END $$

When I try to create it, it gives the Dynamic SQL error:

Error Code: 1336. Dynamic SQL is not allowed in stored function or trigger

I've found solutions involving CONCAT on the web, but that would mean being vulnerable to SQL injection and I obviously don't want that.

Is there something I can do about this? Or is there any other proper way to deal with this situation?

Gordon Linoff

Why are you using dynamic sql?

CREATE FUNCTION get_password_for_user (p_username varchar(30))
    returns varchar(32)
BEGIN
    declare passwd varchar(32) default '';

   select passwd := u.password
   from users u
   where u.name = p_username;

   return passwd;
END $$

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to retrieve dropped stored procedure, function, table in SQL Server 2008

From Dev

How to retrieve value from sql scalar function using c#

From Dev

How to update SQL field using function and stored procedure?

From Dev

How to connect to SQL Server database and retrieve data using Razor in WebMatrix?

From Dev

How to retrieve data from a one to many sql relationship using PHP

From Dev

android: how to retrieve data from sql server using web service

From Dev

How to retrieve data using primary key (INT) by store Procedure in sql?

From Dev

How to stop repeated data using sql query in stored procedure?

From Dev

How to fetch data stored by SQL?

From Dev

Retrieve the returned value from sql server stored procedure using java

From Dev

Retrieve table data from stored procedure using entity framework

From Dev

stored PL/sql function using select statement

From Dev

SQL: using function as parameter to a stored procedure

From Dev

stored PL/sql function using select statement

From Dev

Using a get function to access data stored in a node

From Dev

How to use broom library with purrr while using safely function?

From Dev

How to compare current time to retrieve record stored in SQL

From Dev

How to compare current time to retrieve record stored in SQL

From Dev

How to retrieve list of integers in SQL Server stored procedure?

From Dev

How to retrieve data stored in select2 dropdown?

From Dev

Retrieve data using foreign key SQL

From Dev

ddrescue: How to retry for 1 bad block after all other data is safely stored?

From Dev

Calling a stored procedure using a SQL data source?

From Dev

how to update sql tabel with xml data in sql server 2008 using stored procedure

From Dev

how to Call web service using stored procedure which returns json and store data in table using sql server?

From Dev

how to pass value in jquery function to retrieve the data

From Dev

In SQL Server 2012, how do I get the column name and data type from a view, function, or stored procedure?

From Dev

How to retrieve the data from SQL and display in ModalPopup

From Dev

How to retrieve hierarchical data from SQL table

Related Related

  1. 1

    How to retrieve dropped stored procedure, function, table in SQL Server 2008

  2. 2

    How to retrieve value from sql scalar function using c#

  3. 3

    How to update SQL field using function and stored procedure?

  4. 4

    How to connect to SQL Server database and retrieve data using Razor in WebMatrix?

  5. 5

    How to retrieve data from a one to many sql relationship using PHP

  6. 6

    android: how to retrieve data from sql server using web service

  7. 7

    How to retrieve data using primary key (INT) by store Procedure in sql?

  8. 8

    How to stop repeated data using sql query in stored procedure?

  9. 9

    How to fetch data stored by SQL?

  10. 10

    Retrieve the returned value from sql server stored procedure using java

  11. 11

    Retrieve table data from stored procedure using entity framework

  12. 12

    stored PL/sql function using select statement

  13. 13

    SQL: using function as parameter to a stored procedure

  14. 14

    stored PL/sql function using select statement

  15. 15

    Using a get function to access data stored in a node

  16. 16

    How to use broom library with purrr while using safely function?

  17. 17

    How to compare current time to retrieve record stored in SQL

  18. 18

    How to compare current time to retrieve record stored in SQL

  19. 19

    How to retrieve list of integers in SQL Server stored procedure?

  20. 20

    How to retrieve data stored in select2 dropdown?

  21. 21

    Retrieve data using foreign key SQL

  22. 22

    ddrescue: How to retry for 1 bad block after all other data is safely stored?

  23. 23

    Calling a stored procedure using a SQL data source?

  24. 24

    how to update sql tabel with xml data in sql server 2008 using stored procedure

  25. 25

    how to Call web service using stored procedure which returns json and store data in table using sql server?

  26. 26

    how to pass value in jquery function to retrieve the data

  27. 27

    In SQL Server 2012, how do I get the column name and data type from a view, function, or stored procedure?

  28. 28

    How to retrieve the data from SQL and display in ModalPopup

  29. 29

    How to retrieve hierarchical data from SQL table

HotTag

Archive