how to get mysql function return value in variable using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bhasheer
Forum Newbie
Posts: 1
Joined: Sun Jun 26, 2016 12:22 am

how to get mysql function return value in variable using php

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
Post Reply