Not returning value
Date: 02/14/07
(PHP Community) Keywords: mysql, sql
I have a puzzling problem. I have this function below, used to find an author's numeric id from the author's name. It makes several checks, changing to string for alternate spellings to find a match. The problem is that if it finds a match in the first pass, it returns the number, but if it finds a match in subsequent loops, it doesn't (I had it echoing the information to check) and I don't know why!
I call it with $author_id = get_author($author, 0);
function get_author($author, $loop) {
global $authors_table;
$max = 5;
switch($loop) {
case 0:
$search_author = $author;
break;
case 1:
$search_author = ucfirst($author);
break;
case 2:
$search_author = strtolower($author);
break;
case 3:
$search_author = str_replace("-", " ", $author);
break;
}
$query_author = "SELECT id FROM $authors_table WHERE author = '$search_author'";
$result_author = mysql_query($query_author) or exit(mysql_error());
$count = mysql_num_rows($result_author);
if ($count == 1) {
$author_data = mysql_fetch_assoc($result_author);
return $author_data['id'];
} elseif ($count == 0 AND $loop < $max) {
$new_loop = $loop + 1;
get_author($author, $new_loop);
} else {
return false;
}
}
Does anyone have any idea? Thanks!
Source: http://community.livejournal.com/php/540825.html