You could change this part
$result = mysql_query($sql);
$title = mysql_result($result, 0,0);
echo trim($title);
to
$result = mysql_query($sql) or some_exception_function("ERROR IN QUERY:".$sql."<br>".mysql_error()); // here you can send an email with error, or whatever you want, if some error occurs
$row = mysql_fetch_array($result);
$title = $row['title']; // so you always fetch desired column by it's name
echo trim($title);
and like @fred-ii said, there is no need to close mysql connection