Page 1 of 1

how to get mysql function return value in variable using php

Posted: Sun Jun 26, 2016 12:31 am
by bhasheer
Delimiter $
CREATE FUNCTION FP_USER_EXISTS(xemailid VARCHAR(50)) RETURNS int(11)
BEGIN
SET @User_exists = 0;
SELECT COUNT(*) INTO @found FROM login WHERE emailid = xemailid;
IF @found > 0 THEN
SET @User_exists = 1;
END IF;
RETURN @User_exists;
END $$

function calling in mysql

@return=FP_USER_EXISTS('testing@test.com')
select @return here it shows return value 0 or 1 but in php it is not returing value

$ret=mysqli_query($connection,"select FP_USER_EXISTS('$emailid')");
if($ret==1)
{
echo "User already exits";
}
else
{
echo 'Inserted successfully';
}
every time it is showing one value based on condition

Thanks

Re: how to get mysql function return value in variable using

Posted: Sun Jun 26, 2016 4:34 am
by requinix
What you're doing boils down to just a simple SELECT query. So you have to work with it the same way you would work with other SELECT queries: execute and fetch a row.

Code: Select all

$ret=mysqli_query($connection,"select FP_USER_EXISTS('$emailid')");
list($user_exists) = mysqli_fetch_row($ret);
if($user_exists == 1)
{
You should also be using prepared statements because of that email address.