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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Noob question on Functions 1

Status
Not open for further replies.

gpalmer711

IS-IT--Management
May 11, 2001
2,445
GB
Hi All,

Sorry if this is a little basic but I am quite new to PHP.

I'm currently trying to make a custom front page for a clients phpBB forum and am going through the PHP code and using what I need.

I have the code working as I want it to, but now i'm trying to streamline the page so that it loads faster. There is a block of code that is being used over and over so I wanted to put it into a function. However when I try I get various errors, I think that these are relating to variables that are declared in files that are included using the include statement. Here's the code

Code:
function greg($forum_id){
$sql = "SELECT t.*, u.username, u.user_id, u2.username as user2, u2.user_id as id2, p.post_username, p2.post_username AS post_username2, p2.post_time 
	FROM " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2, " . USERS_TABLE . " u2
	WHERE t.forum_id = $forum_id
		AND t.topic_poster = u.user_id
		AND p.post_id = t.topic_first_post_id
		AND p2.post_id = t.topic_last_post_id
		AND u2.user_id = p2.poster_id 
		AND t.topic_type <> " . POST_ANNOUNCE . "
		AND t.topic_type <> " . POST_STICKY . " 
		$limit_topics_time
	ORDER BY t.topic_type DESC, t.topic_last_post_id DESC 
	LIMIT $start, 1";
if ( !($result = $db->sql_query($sql)) )
{
   message_die(GENERAL_ERROR, 'Could not obtain topic information', '', __LINE__, __FILE__, $sql);
}
}

It errors out on this line

Code:
if ( !($result = $db->sql_query($sql)) )

The error code is

Code:
Fatal error: Call to a member function sql_query() on a non-object in c:\Inetpub\[URL unfurl="true"]wwwroot\phpBB2\greg1.php[/URL] on line 59

So i tried adding the code

Code:
global $db;

at the start of the original block. Then get the following error

Code:
SQL Error : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1' at line 12

which relates to the number 1 at the end of my SQL statement.

So I guess what I am really asking is, is this just a silly noob question from not understanding functions correctly in PHP or something else?

Thanks for looking

Greg Palmer
Freeware Utilities for Windows Administrators.
 
It looks to me like you build your query using the variables $forum_id, $limit_topics_time and $start.

However, I do not see mention of your having used the global operator on these variables, not do I see code inside the function which initializes them.

I suspect that PHP is generating warnings on these variable references that it is not showing you. I recommend that in php.ini on development boxes, you set [tt]error_reporting[/tt] to [tt]E_ALL[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks for that sleipnir214

I already have the php.ini file set to E_ALL.

You post did make me realise that it was a simple case of adding global references for $limit_topics_time and $start

Thanks for that and a star for you

Greg Palmer
Freeware Utilities for Windows Administrators.
 
Interesting.

I would have thought that an attempt to use in a string a variable that had not been initialized would have generated a warning or notice. And an error_reporting setting of E_ALL should have configured PHP to display that warning.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Well the strange thing is that both $limit_topics_time and $start are initialized in another file that is included using the include statement. If I don't put the code in a function then the it works fine without having to add the global declarations.

I guess I need to do alot more reading on how functions work, and php in general [bigsmile]

Thanks again

Greg Palmer
Freeware Utilities for Windows Administrators.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top