Page 1 of 1

Using same php several times within a page (multiple DB)

Posted: Fri Apr 24, 2009 6:16 am
by mnematv
Hi, I have a php script that works well for my forum. The only thing is I wan't to use it several times within my main page. I would like to include several different slighlty modified versions to get the desired output in different sections.
My problem is that including this code as is several times attempts to open multiple connections to the database and I get an error "Warning: mysql_connect() [function.mysql-connect]: Access denied for user "...

How can I modify the code so that I can reuse different versions of this file within my page? Thanks!

Code: Select all

<? php
// change to the path to your forum 
$path = './forum/'; 
 
// amount of topics to show 
$amount = 5; 
 
// forum ids to NOT show topics forum, separated by commas 
$exclude = ''; 
 
// ------ no need to edit below ------ // 
 
require_once("{$path}config.php"); 
 
$conid = mysql_connect($dbhost, $dbuser, $dbpasswd);  
$db    = mysql_select_db($dbname, $conid); 
mysql_query("set names 'cp1251';");                     
 
 
unset($dbpasswd); 
 
$sql_where = 'WHERE `forum_id` > -1 ';  
 
if($exclude != '')  
{      
    $exclude_forums = explode(',', $exclude);  
    foreach ($exclude_forums as $id)  
    {  
        if ($id > 0)  
        {  
            $sql_where .= ' AND `forum_id` <> ' . trim($id);  
        }  
    }  
}  
 
 
 
 
$sql = "SELECT `topic_id`, `topic_title` FROM `{$table_prefix}topics`  {$sql_where} ORDER BY `topic_time`DESC LIMIT 0, {$amount}"; 
$result = mysql_query($sql, $conid); 
 
$topics_html = ''; 
while($topic = mysql_fetch_array($result)) 
{ 
    $topics_html .= "<a href=\"{$path}viewtopic.php?t={$topic['topic_id']}\">{$topic['topic_title']}</a><br />"; 
} 
mysql_close($conid); 
 
echo $topics_html; 
?>

Re: Using same php several times within a page (multiple DB)

Posted: Fri Apr 24, 2009 9:26 am
by Reviresco
It looks like this is the culprit:

Code: Select all

unset($dbpasswd);
since it is not being reset anywhere.

You only need the db connect and disconnect stuff once each. Put the connect stuff at the top:

Code: Select all

// ------ no need to edit below ------ //
 
require_once("{$path}config.php");
 
$conid = mysql_connect($dbhost, $dbuser, $dbpasswd);  
$db    = mysql_select_db($dbname, $conid);
unset($dbpasswd);
 
 
and this after all the queries are done:

Code: Select all

 
mysql_close($conid);
 

Re: Using same php several times within a page (multiple DB)

Posted: Fri Apr 24, 2009 1:09 pm
by mnematv
Thanks for the reply, however can you please clarify what I need to do. I don't see how moving the code to the top will resolve this issue or should I completely remove
unset($dbpasswd);. or maybe I wasnt completely clear.

Imagine if I have several files file1.php file2.php file3.php which are identical, and I am trying to include them all in my page. They all connect to the same db (probably at the same time)

Re: Using same php several times within a page (multiple DB)

Posted: Fri Apr 24, 2009 3:29 pm
by Reviresco
If it's the same database, you don't have to reconnect to it each time on the same page. Remove the db connect stuff from those files and just include it once on the page before the queries. Put the db close function after you're done querying.

It looks like what's happening is you're running the db connect for each file. This is not a problem (but it is unnecessary -- just leave the same connection open) -- but if you are unsetting the password, after the first time through, it can't reconnect because the password is gone.

Summary:

1. Take out db connect code from file1.php, file2, php, etc.
2. Put db connect code just once at top of page.
3. Don't close the db connection yet. Also no need to unset the password.
4. Include file1.php, file2.php, etc.
5. Close the db connection at end of page.