Forum Moderators: coopster

Message Too Old, No Replies

Connect to DB multiple times?

         

smatts9

5:02 pm on Nov 21, 2008 (gmt 0)

10+ Year Member



I have a page where I populate a select field with information from a database and the user inputs other info and then at the end I input the data into another table.

I have something like:

<? session stuff ?>
HTML
<? populate menu ?>
HTML
<? insert into.. ?>

-------

I have tried putting: include 'db.php'; up top with the session but it won't connect the two times I need it and even if I just put it with the populate menu code it will only work there and not in the "insert into" area.

I also have a variable I define in the populate menu area which I need to redefine again in the insert into area for it work.

Is there something I am missing because this seems very redundant?

Thanks.

PHP_Chimp

6:40 pm on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put all of your php code together on the page.
Dont keep closing and opening php within the same page.
As your variables are going out of scope [us2.php.net].

cameraman

11:51 pm on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When "it won't connect" what error message are you receiving?

variable scope: php doesn't care about switching between php and html 'mode' with regard to variable scope. Even variables in other included files have the same scope provided they're outside function blocks.

<?php $a = 'Alexander'; ?>
<p>hello&nbsp;
<?php echo $a; // same scope ?>
</p>
<div>I said, Hello <?php echo $a; // same scope ?>!</div>
<?php
function foo() {
echo $a; // Different scope
}
?>
<hr>
<?php include "file_that_uses_or_assigns_variable_a_outside_any_function.php"; // same scope ?>

HoboTraveler

8:08 am on Nov 22, 2008 (gmt 0)

10+ Year Member



Use the TRUE parameter. Like this:

$db = mysql_connect (DB_HOST, DB_USER, DB_PASS, TRUE) or die("Could not connect");

eeek

2:44 am on Nov 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why do you need two connections?

smatts9

3:31 pm on Nov 25, 2008 (gmt 0)

10+ Year Member



Chimp's suggestion worked by echoing all of the html rather than stop and start the PHP brackets.

eeek: I don't need two connections. But I have 2 mysql queries and the first one worked but the second one would not and only would if I included an include(db.php) right above it.

cameraman: I would receive I believe something along the lines of "ODBC cant connect ...", but I don't exactly recall.