ok so this is strange as hell. I'm coding a portal for IPB 1.x for my site, but i'm connecting to two diff db's. I named my connections $myref and $njDB. The connections are declared once and called from a template file.

Now, in tmpl_top and index ( which calls tmpl_top, has some code, then calls tmpl_foot ) the Link identifier varialbles work properly and the queries execute.

However, once i get to tmpl_foot something goes wrong. I had to switch the order in which i declared my connections, and had to remove the variable for the link from the queries in the two functions that were bombing out. This is odd though because all other queries on the site work fine using the link identifier.

Here's what i mean....

this is the block of code where i defined my connections. Previously Sharelive was declared first and NJ was declared second. However for some reason or another it made the scipt go nuts....

include_once("Variables/slvars.php");
// Connect to Napjunk Database
// Make connection to database
$njDB =mysql_pconnect($dbhost,$dbuser,$dbpass) or
	die(mysql_error());
mysql_select_db($dbname,$njDB) or
	die(mysql_error());

// Conect to Sharelive Database
$myref = mysql_pconnect($hostname_Default, $username_Default, $password_Default) or die(mysql_error());
mysql_select_db($database_Default,$myref) or
	die(mysql_error());

Then i had to go into my functions and remove the link identifier for it to work. However even having switched the order in which the connections are declared if i use a l ink identifier variable it will cause the script to bomb out.

why the hell is PHP loosing only ONE of the L-ID variables?

did i explain this well enough, or do i need to elaborate or provide more sample code...

    You should provide us with more of the code and please mark there what exactly is not working.

    Z.

      ok here it goes....

      these are the two functions that don't work if i KEEP the link identifier varialbe, if I remove them it works for some mysterious reason or another but I had to swap the order in which i declared my DB connections in the code i mentioned above. I think it's getting closed somewhere, but it shouldnt since it's soposed to be a persistent connection.

      	// Recent Topics Function
      	function recenttopics()
      	{
      	$query = mysql_query("SELECT * FROM `shlinvtopics` WHERE `forum_id`='53' ORDER BY last_post DESC LIMIT 0 , 10",$myref) or die(mysql_error()); <---this line gets an error....
      	while($row = mysql_fetch_array($query))
      		{
      			$topid = $row['tid'];
      			$toptitle = $row['title'];
      			echo "<img src=\"arrow1.gif\"><a href=\"http://www.sharelive.com/1.0/forums/index.php?showtopic=".$topid."\"><font size=\"1\" class=\"sf\"  face=\"verdana, arial, helvetica\">".$toptitle."</font></a><br>";
      		}
      	return;
      	}
      
      // Older News List Function
      function listoldnews()
      	{
      		$r = mysql_query("SELECT `title`, `tid` FROM `shlinvtopics` WHERE `forum_id` = '55' AND `approved` = '1' ORDER BY `start_date` DESC LIMIT 3, 10",$myref) or die(mysql_error()); <---this line of code too....
      		while($row = mysql_fetch_array($r))
      				{
      					$titlelen = strlen($row['title']);
      					if($titlelen >= 30)
      						{
      						$fspace = strpos($row['title'], " ", 25);
      						$row['title'] = substr($row['title'],0,$fspace);
      						$row['title'] = $row['title'] . "...";
      						}
      					echo "<img src=\"arrow1.gif\"><a href=\"article.php?pid=" . $row['tid'] ."\"><font size=\"1\" class=\"sf\" face=\"verdana, arial, helvetica\">" . $row['title'] . "</font></a><br>";
      				}
      	}
      

      does that help?

        if it helps i have ziped all the files from the portal that relate to this problem. My code is a bit disorganized at the moment, so if your head pops while reading my code i apologise in advance. I've had about 4 other people who've done code modifications on the portal and it can be annoying at times...Trying to fix all this is how i broke the damn thing in the first place.

        Download portal source code ( 10.1 Kb )

        btw, here's the url to the test page,

          Have you ever read about the scope of variable validity ?

          It means that variables are not valid in the function scope unless it is declared as a global variable in the function. Then the variable $myref is empty in both function and then the PHP uses last connection id. So then it depends on the order of mysql_pconnect function. Try this little trick as a first row in each of that two function write down this:

              global $myref;
          

          like this:

          function recenttopics() 
          { 
              global $myref;
          
          .
          .
          .
          }
          

            hmmm...leme tinker with that for a minute...this is interesting. lol

              that's awesome...

              can that be aplied while declaring my variables originaly or do I have to do that within each function i use them in?

                Because of the philosophy it must be done in each function, where you are using such a variable or better way is that each function which uses connection to DB has a parameter where it is passed so you have two possibilities:

                1)

                function display_something () {
                    global $my_conn;
                    $query = mysql_query("SQL Here",$my_conn);
                    ....
                }
                
                $my_conn = mysql_pconnect( ... );
                display_something();
                

                2)

                function display_something($conn) {
                    $query = mysql_query("SQL Here",$conn);
                }
                
                $my_conn = mysql_pconnect( ... );
                display_something($my_conn);
                

                Zdenek

                  interesting. Thanks a whole lot, you just helped me fix about 15 bugs in one swoop...

                    That is why I am here ;-) Please mark this thread as [Resolved]

                      Write a Reply...