Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed returning values from user function.

Status
Not open for further replies.

peterv12

Technical User
Dec 31, 2008
108
US
I've written some code that works exactly the way I want it to. Now that I've got it that way, I want to put it in a user defined function so that I can call it from multiple places. Unfortunately, I haven't been able to figure out how to return the values from the function to the calling code. I've used the return command, but still got nothing. I've included the code below:

Code:
//************  S E A R C H  ************
  case 'Search':
//

  if (($_POST['title']) != NULL) {
      $record_key = $_POST['title'];
      $field = 'title';
  }else{
      $field = 'bookid';
      $record_key = $_POST['bookid'];
  }

  $query = "SELECT * FROM MYBOOKS where $field like '%$record_key%'";

//
//  $query = "SELECT * FROM MYBOOKS where title like '%$record_key%' ";

  $result = mysql_query($query);

  $rows=mysql_affected_rows(); // will return the number of rows produced by the query.
  if (!$result) {
     $message  = 'Invalid query: ' . mysql_error() . "\n";
     $message .= 'Whole query: ' . $query;
     die($message);
  } else {
     if ($rows > 0) {
        while ($_POST = mysql_fetch_array($result, MYSQL_ASSOC)) {
           $bookid  = $_POST['bookid'];
           $authorid = $_POST['authorid2'];
           $title = $_POST['title'];
           $type = $_POST['type'];
           $cover_type = $_POST['cover_type'];
           $pages = $_POST['pages'];
           $copyright = $_POST['copyright'];
           $date_finished = $_POST['date_finished'];
           $has_been_read = $_POST['has_been_read'];
        }
        break;
     }
  }

I call the function like this, and the echos after the return print blank:
Code:
 test1("$title");

 echo "title: $title<br>";

Then the function looks like this (basically the same as the code above):
Code:
//****** FUNCTION
function test1($title, $bookid) {
   echo "Parameters: $title $bookid<br>";

//************  S E A R C H  ************
//
  if (($_POST['title']) != NULL) {
      $record_key = $_POST['title'];
      $field = 'title';
  }else{
      $field = 'bookid';
      $record_key = $_POST['bookid'];
  }

  $query = "SELECT * FROM MYBOOKS where $field like '%$record_key%'";

  $result = mysql_query($query);

  $rows=mysql_affected_rows(); // will return the number of rows produced by the query.
  if (!$result) {
     $message  = 'Invalid query: ' . mysql_error() . "\n";
     $message .= 'Whole query: ' . $query;
     die($message);
  } else {
     if ($rows > 0) {
        while ($_POST = mysql_fetch_array($result, MYSQL_ASSOC)) {
           $bookid  = $_POST['bookid'];
           $authorid = $_POST['authorid2'];
           $title = $_POST['title'];
           $type = $_POST['type'];
           $cover_type = $_POST['cover_type'];
           $pages = $_POST['pages'];
           $copyright = $_POST['copyright'];
           $date_finished = $_POST['date_finished'];
           $has_been_read = $_POST['has_been_read'];
           [b][COLOR=red]return $title;[/color][/b]
        }
//        break;
     }
  }
echo "rows: $rows<br>";
echo "title: $title<br>";
echo "bookid: $bookid<br>";
}

One VERY important thing I need to know is how you can return multiple values from a function. I need to return all the values from the query.

If someone has the time and interest, I'd really appreciate any assistance you might give. I'll be banging away at this until I figure it out. Thanks in advance.
Peter V.
 
Never mind all, after an extensive web search, I found a solution. It involves returning an array from the function.
First the call:
Code:
//******  U P D A T E  ******
  case 'Update':
     list ($rows, $bookid, $authorid2, $title, $type, $cover_type, $pages, $copyright, $date_finished, $has_been_read) = test1($title,$bookid);
     break;

Then the function:
Code:
while ($_POST = mysql_fetch_array($result, MYSQL_ASSOC)) {
           $bookid  = $_POST['bookid'];
           $authorid = $_POST['authorid2'];
           $title = $_POST['title'];
           $type = $_POST['type'];
           $cover_type = $_POST['cover_type'];
           $pages = $_POST['pages'];
           $copyright = $_POST['copyright'];
           $date_finished = $_POST['date_finished'];
           $has_been_read = $_POST['has_been_read'];
[b][COLOR=red]return array ($rows, $bookid, $authorid2, $title, $type, $cover_type, $pages, $copyright, $date_finished, $has_been_read)[/color][/b];

I just thought I'd put the solution in in case anyone in the future can use this info.
Peter V.
 
please do NOT use $_POST in this way. you should never assign values to the POST/GET superglobals yourself. very bad practice.

typically people use $row to designate rows in a recordset.

in your function, you need not use a while loop if you are supplying a primary key. as only one record will ever be provided.

and of course it is pointless to split out the array only to return it.

i suggest this instead (although i do not understand how you can guarantee a unique value by a search on title).

Code:
function test1($title = null, $bookid=null){
  $sql = 'select rows, bookid, authorid2, title, type, cover_type, pages, copyright, date_finished, has_been_read from sometable ';
  $where = empty($bookid) ? "where title='%s'" : "where bookid=%d";
  $query = sprintf($sql . $where, mysql_real_escape_string(empty($bookid) ? $title : $bookid));
  $result = mysql_query($query);
  return ($result ===false) ? false : mysql_fetch_array($result, MYSQL_NUM);
}
the function will return an array you can use with list() or false. false will designate either a bad query or an empty result set.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top