Avatar of namsu55
namsu55Flag for United Kingdom of Great Britain and Northern Ireland asked on

PHP Function not Working

My code works outside of a function, but when using a function it doesnt work. I even tried a suggested solution of writing return null at the end of the function but it did not work, can you please assist me.
function myFunction()
{
$tablename = "login";
$sql = "SELECT * FROM $tablename WHERE username = '$_POST[username]'";
$result = @mysql_query($sql, $connection) or die(mysql_error());
$num = mysql_num_rows($result);
 
 
 
if ($num != 0) 
 
{
 
 
 
echo "<h5>Logged in Username has Already been taken</h5>";
 
 
} 
 
 
 
}
 
myFunction;

Open in new window

PHP

Avatar of undefined
Last Comment
pvginkel

8/22/2022 - Mon
pvginkel

Could you try the following sample. I'vemade a change to the last line and the $sql line.
function myFunction()
{
    $tablename = "login";
    $sql = "SELECT * FROM $tablename WHERE username = '" . $_POST["username"] . "'";
    $result = @mysql_query($sql, $connection) or die(mysql_error());
    $num = mysql_num_rows($result);
 
    if ($num != 0) 
    {
        echo "<h5>Logged in Username has Already been taken</h5>";
    } 
}
 
myFunction();

Open in new window

ASKER
namsu55

I tried that already with the brackets, I removed them before because I thought maybe it would work.
pvginkel

Have you tried the change to the line starting with $sql = ? That line also contained a problem.

What you should probably also try is adding the following like after the line with mysql_query:

echo mysql_error();

This may return an error from your sql query.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
namsu55

That alll works outside of a function, no problems with that.
ASKER CERTIFIED SOLUTION
Cornelia Yoder

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
pvginkel

Yes, that's it. Stupid I didn't see it :).