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:
I call the function like this, and the echos after the return print blank:
Then the function looks like this (basically the same as the code above):
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.
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.