0

The same code used as below works fine, but when you un-comment the function and try to call the function, it does not behave the same? Am trying to standardize the CRUD using functions, but running into issues.

  1. The print_r does not display the output when called inside a function but works when called as is outside of a function
  2. the return value does not return when called via a function
  3. I would like to get the return values via the function and decide what to do next.

Appreciate if you could help me out. Thanks

<?php
    //function verifyemail($p_email) {
        echo "insde function - " . $p_email;
        try {
            //      $p_email = "r@gmail.com";
            include 'db.php';
            connectDB('msme_db',1) ;
            $sql = "select count(*) as p_exists from    msme_users where user_email = '$p_email' ;" ;
            echo $sql ;
            $result = $__conn->prepare($sql);
            $result->execute();
            $result->setFetchMode(PDO::FETCH_ASSOC);
            $row = $result->fetch() ;
            //print_r ($row);
            if ($row)
            {
                $i = $row['p_exists'] ;
                return $row !== false ? $row : 'x';
            } 
       } catch (PDOException $e) {
            echo  " in catch" ;
            die("Error occurred:" . $e->getMessage());
       }
//} // End of Function

//$email = "r@gmail.com";
//echo sprintf('Email %s is %s', $mail, verifyemail($email)) ;
print_r($row) ;
?>
Jeff
  • 6,895
  • 1
  • 15
  • 33
Ramani
  • 1
  • `$__conn` might be undefined when inside a function (because of scope). Is it defined in `db.php`? See this answer: https://stackoverflow.com/a/2619578/4830296 – Jeff Sep 27 '18 at 20:29
  • And if `fetch()` fails the function doesn't return anything – msg Sep 27 '18 at 20:33
  • do a `var_dump($__conn);` before you use it to see if it's defined. – Jeff Sep 27 '18 at 20:37
  • also the return value will be an array ($row), so you'll get the `NOTICE Array to string conversion on line number x` for `sprintf('Email %s is %s', $mail, verifyemail($email))` – Jeff Sep 27 '18 at 20:41

1 Answers1

0

Problems I see: 1. you have return command in if statement

if ($row)
{
    $i = $row['p_exists'] ;
    return $row !== false ? $row : 'x';
}

so if is true then you return from function and if is not true then you don't
check this by putting echo inside and see if you see anything

if ($row)
{
    echo 'IF Statemen in line: ' . __LINE__ . '<br>' . PHP_EOL;
    $i = $row['p_exists'] ;
    return $row !== false ? $row : 'x';
}

you should rewrite the code so you always return form the function and use if statement only to assign value to $i or $row
ie like that:

function verifyemail($p_email) {
    echo "insde function - " . $p_email;
    try {
        //      $p_email = "r@gmail.com";
        include 'db.php';
        connectDB('msme_db',1) ;
        $sql = "select count(*) as p_exists from    msme_users where user_email = '$p_email' ;" ;
        echo $sql ;
        $result = $__conn->prepare($sql);
        $result->execute();
        $result->setFetchMode(PDO::FETCH_ASSOC);
        $row = $result->fetch() ;
        //print_r ($row);
        if ($row)
        {
            $i = $row['p_exists'] ; //<== where do you use $i?
            $row !== false ? $row : 'x';
        } 
   } catch (PDOException $e) {
        echo  " in catch" ;
        die("Error occurred:" . $e->getMessage());
   }
   return $row; //<== here you rentrun from the function
} 
// End of Function
// ^ here is place for the comment, not in the line where } is

//$email = "r@gmail.com";
//echo sprintf('Email %s is %s', $mail, verifyemail($email)) ;
print_r($row) ;
?>
  1. If you use return statement without the function body so in the global code scope you end the php script execution
    so the code

    print_r($row) ;
    

is never executed

to summarize - put echo statements

echo 'line: ' . __LINE__ . '<br>' . PHP_EOL;

into if statement and other places and check numbers with the code and you'll probably see what you havn't seen where your execution flows where not.
and move return statement outside if statement, perfectly at the end of the function.

Jimmix
  • 5,644
  • 6
  • 44
  • 71
  • this `return $row;` could result in an uncatched error '$row is undefined' – Jeff Sep 27 '18 at 22:03
  • you're right this `return $row;` assumes there is always a value of `$row` so `$row` should have been assigned with default value before or `return $row;` be in if statement at the end where you return value of `$row` or default value ie `false` – Jimmix Sep 27 '18 at 22:16