/    Sign up×
Community /Pin to ProfileBookmark

Problem accessing multiple databases, not at the same time..

I have 2 scripts. accessControl.php controls session information, and formNote.php is a form creation script. accessControl is an include in formNote.php, and will be in subsequent pages. The way this should work is that accessControl access a database and then verifies membership, then passes control back to formNote. formNote then accesses a seperate database to get some headers for a drop down menu. For some reason it won’t access the proper database in formNote. it still tries to connect to the first database.

Any ideas?

[code]

<? //formNote.php
session_start();
include ‘db.php’;
include ‘accessControl.php’;
?>

<html>
<head>
<title>MyNotes Entry Page</title>
</head>

<body>
<form method=”post” action=”procForm.php”>
<table>
<tr>
<td>Topic Title</td>
<td><input type=”text” name=”topicName” size=”50″ maxlength=”50″></td>
</tr>

<tr>
<td valign=”top”>Content</td>
<td><textarea name=”topicBody” rows=”10″ cols=”65″></textarea>
</tr>

<tr>
<td>Store Content as</td>
<td>
a Subtopic of Page (or as a New Topic)

<?
dbConnect(“jmcclure_MyNotes”);
$queryA = “SELECT topicId, topicName FROM topicsTable WHERE topicParent = ‘NULL'”;
$result = mysql_query($queryA) or die (error(mysql_error()));

print “<select name=’topicParent’>”;
print “<option value = ‘NULL’>New Page</option>”;
/*while($row = mysql_fetch_assoc($result)){
$topId = $row[‘topicId’];
$topName = $row[‘topicName’];
print “<option value = $topId>$topName</option>”;
}*/
while($row = mysql_fetch_assoc($result)){
print “<option value=”{$row[‘topicId’]}”>{$row[‘topicName’]}</option>n”;
}
?>

</td>
</tr>

<tr>
<td><input type=”submit” value=”Process Page”>
</tr>
</table>
</form>

</body>
</html>
[/code]

[code]

<? //accessControl.php
session_start();
include_once ‘common.php’;
include_once ‘db.php’;

$uid = isset($_POST[‘uid’]) ? $_POST[‘uid’] : $_SESSION[‘uid’];
$pwd = isset($_POST[‘pwd’]) ? $_POST[‘pwd’] : $_SESSION[‘pwd’];

//if this is first visit to site, require login
if(!isset($uid)){
?>
<html>
<head>
<title>Please Login for Access</title>
</head>
<body>
<h1>Login Required</h1>
<p>You must log in to access this area of the site. If you are
not a registered user, <a href = “signup.php”>click here</a>
to signup for instant access.</p>
<p><form method =”post” action=”<?=$_SERVER[‘PHP_SELF’]?>”>
User ID: <input type=”text” name=”uid” size=”8″><br>
Password: <input type=”password” name=”pwd” size=”8″><br>
<input type=”submit” value=”Log In”>
</form></p>

</body>
</html>

<?
exit;
}

$_SESSION[‘uid’] = $uid;
$_SESSION[‘pwd’] = $pwd;

//match uid and pwd to stored username and password
dbConnect(“jmcclure_sessions”);
$query = “SELECT * FROM user WHERE
userID = ‘$uid’ AND password = ‘$pwd'”;
$result = mysql_query($query);
if (!$result){
error(‘A database error occured while checking your ‘.
‘login details.\nIf this error persists, please ‘.
‘contact [email protected]’);
} //end dbError if

//if uid or pwd not found, reset uid and pwd and try again
if (mysql_num_rows($result) == 0){
unset($_SESSION[‘uid’]);
unset($_SESSION[‘pwd’]);
?>

<html>
<head>
<title>Access Denied</title>
</head>

<body>
<h1>Access Denied</h1>
<p>Your user ID or password is incorrect, or you are not a
registered user on this site. To try logging in again, click
<a href=”<?=$_SERVER[‘PHP_SELF’]?>”>here</a>. To register for instant
access, click <a href=”signup.php”>here</a>.<p>

</body>
</html>
<?
exit;
} //end uid pwd not found if

$userName = mysql_result($result,0,’fullname’);
?>

[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 16.2005 — did you close the first connection?
<i>
</i>mysql_close ( [resource link_identifier])
Copy linkTweet thisAlerts:
@chazzyNov 16.2005 — [code=php]
print "<select name='topicParent'>";
print "<option value = 'NULL'>New Page</option>";
/*while($row = mysql_fetch_assoc($result)){
$topId = $row['topicId'];
$topName = $row['topicName'];
print "<option value = $topId>$topName</option>";
}*/
while($row = mysql_fetch_assoc($result)){
print "<option value="{$row['topicId']}">{$row['topicName']}</option>n";
}
?>
[/code]


i feel like there should be some sort of </select> tag in there somewhere near the end.

as lilcrazy put, i think you need a mysql_close().

you can alleviate that with prefixing your database name to the table's you want to look at, so

jmcclure_MyNotes.topicsTable and

jmcclure_sessions.user
Copy linkTweet thisAlerts:
@poisedforflightauthorNov 16.2005 — chazzy -

i tried adding the </select> with the same results.

where would I include the mysql_close ( [resource link_identifier])? in my accessControl, formNote or db.php, which handles the dbConnect function?

btw here is the db.php

<i>
</i>&lt;?php //db.php

function dbConnect($db=""){
//global $dbHost, $dbUser, $dbPass;

define('DB_NAME', $db); // The name of the database
define('DB_USER', 'user_name'); // Your MySQL username
define('DB_PASSWORD', 'pass_word'); // ...and password
define('DB_HOST', 'localhost'); // 99% chance you won't need to change this value

$connection = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Could not connect to mysql.");
$db_handle = mysql_select_db(DB_NAME,$connection) or die("Could not select database.");

return $connection;

} //end dbConnect()

?&gt;


username and password changed to protect the semi-innocent
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 16.2005 — try this
<i>
</i>&lt;? //formNote.php
session_start();
include 'db.php';
include 'accessControl.php';
?&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;MyNotes Entry Page&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form method="post" action="procForm.php"&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;Topic Title&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="topicName" size="50" maxlength="50"&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td valign="top"&gt;Content&lt;/td&gt;
&lt;td&gt;&lt;textarea name="topicBody" rows="10" cols="65"&gt;&lt;/textarea&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Store Content as&lt;/td&gt;
&lt;td&gt;
a Subtopic of Page (or as a New Topic)

&lt;?
dbConnect("jmcclure_MyNotes");
$queryA = "SELECT topicId, topicName FROM topicsTable WHERE topicParent = 'NULL'";
$result = mysql_query($queryA) or die (error(mysql_error()));

print "&lt;select name='topicParent'&gt;";
print "&lt;option value = 'NULL'&gt;New Page&lt;/option&gt;";
/*while($row = mysql_fetch_assoc($result)){
$topId = $row['topicId'];
$topName = $row['topicName'];
print "&lt;option value = $topId&gt;$topName&lt;/option&gt;";
}*/
while($row = mysql_fetch_assoc($result)){
print "&lt;option value="{$row['topicId']}"&gt;{$row['topicName']}&lt;/option&gt;n";
}
[B][COLOR=Red]mysql_close($connection);[/COLOR][/B]
?&gt;

&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;input type="submit" value="Process Page"&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@chazzyNov 16.2005 — chazzy -

i tried adding the </select> with the same results.

[/QUOTE]


You should have, that wasn't blocking the page from running but it was an error in your HTML. Did you try prefixing the database name to the table like I recommended as well?
Copy linkTweet thisAlerts:
@felgallNov 16.2005 — Why have two databases? You can achieve the same effect by using separate tables in the one database. The whole idea of a database is that there is ONE central data store.
×

Success!

Help @poisedforflight spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...