title: how to return mysql data from a php function
body: <p>I have a php function that I'm hoping will return all data. The problem right now is it will return the first row called then hang out and I'm not sure why. </p>
  
  <pre><code>    function get_user_info($id,$field='') 
  {
      //connect to database
      include_once("../config.php");
      $qry= "SELECT `$field` FROM `".$user_table."`  WHERE `id` ='$id'";
      //die($qry);
      $result=mysql_query($qry);
      if($result) {
          if(mysql_num_rows($result)&gt;0) {
              //query successful 
              $data=mysql_fetch_row($result);
              $user_info= $data[0];
          }
          else{
              $user_info ="invalid field";
          }
  
      }
      else{
          $user_info="invalid data";
          }
      return $user_info; 
  }
  </code></pre>
  
  <p>If I run a function like echo <code>(get_user_info($user_id,'username')." ".get_user_info($user_id,'username'));</code> It returns <code>username invalid data</code> For the life of me I can't figure out why it won't get the data assocated with each row of <code>get_user_data</code></p>
  
  <p><strong>EDIT:</strong> It seems to be dying at <code>$result=mysql_query($qry);</code> which returns Incorrect table name ''</p>
  
  <p><strong>EDIT2:</strong> Looks like the problem was that I was only including the config file once and using a variable for my table name which was getting unset when I tried to call it a second time. </p>
  
  <p><strong>EDIT 3:</strong> 
  Here is the final function </p>
  
  <pre><code>//connect to database
  if(file_exists('..config.php')){
      include_once("../config.php");
  }
  function get_user_info($id,$field) 
  {
  
      //get users table
      global $user_table_name;
  
      $qry= "SELECT `$field` FROM `$user_table`  WHERE `id` ='$id'";
      $result=mysql_query($qry) or die(mysql_error());
      if($result) {
          if(mysql_num_rows($result)&gt;0) {
              //query successful 
              $data=mysql_fetch_row($result);
              $user_info= $data[0];
          }
          else{
              $user_info =$qry;
          }
  
      }
      else{
          $user_info="invalid data";
          }
      return $user_info; 
  }
  </code></pre>