Using same php several times within a page (multiple DB)
Posted: Fri Apr 24, 2009 6:16 am
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!
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;
?>