-1
  $employee_id='E81OYKC';   // here is my employee id 

  getFileName($employee_id);  // here i am calling function

  echo $response; // here i want $response value.

  function getFileName($employee_id)
  {

      //Mysql Query to fetch data 

       $sql = mysql_query("SELECT work_experience, curr_designation from employee_registration where employee_id='$employee_id'") or die(mysql_error());

       //fetching data from server 

      while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) 
      {

        $curr_des = $row['curr_designation'];
        $work_exp = $row['work_experience'];
        $response =  $work_exp.' '.$curr_des;
        return $response;  // here is my result
      }
 }

//here in this code i want $response value after calling method i.e getFileName() but i am getting blank value over there.

Yuvraj
  • 3
  • 5
  • `return` leaves a function, so your while loop will only perform one iteration, and then exit the function. You want to _append_ new content to your `$response` variable in the loop, and then return it _after_ the loop. – CBroe Jun 09 '17 at 11:12
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 09 '17 at 11:14
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jun 09 '17 at 11:14
  • In addition to what @CBroe and @Jay Blanchard said: Just calling the function will execute what is inside and return the value, everything ok with that. But you have to store what the function returns, in a variable, in this case call it `$response`. – Tobias F. Jun 09 '17 at 11:15

2 Answers2

0

Receive your response into variable then you can use it

 $response = getFileName($employee_id);  // here i am calling function

 echo $response;
B. Desai
  • 16,414
  • 5
  • 26
  • 47
-1

you need variable to get the response for example

$response = getFileName($employee_id);

then you print_r or echo $response

and make sure don't used mysql_* . you can used mysqli_*

i have done some changes.. try this

$employee_id='E81OYKC';   // here is my employee id 

      $response = getFileName($employee_id);  // here i am calling function

      echo $response; // here i want $response value.

      function getFileName($employee_id)
      {

          //Mysql Query to fetch data 

           $sql = mysql_query("SELECT work_experience, curr_designation from employee_registration where employee_id='$employee_id'") or die(mysql_error());

           //fetching data from server 

          while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) 
          {

            $curr_des = $row['curr_designation'];
            $work_exp = $row['work_experience'];
            $response =  $work_exp.' '.$curr_des;
            return $response;  // here is my result
          }
     }
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42