By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,313 Members | 1,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,313 IT Pros & Developers. It's quick & easy.

Session Cannot Always Access Database

P: n/a
I have a site where a user logs in and a session variable I created
is used to keep track of the fact that the user is logged in. This
various pages query a MySQL database to get information (mostly to
generate a catalogue and news page). Occasionally when loading a page I
simply get the error "No database selected" when the PHP script attempts
to run the query. Usually I can press reload in my browser (Netscape,
IE, and Opera) and the page will work. Sometimes it won't. What may be
causing this error? The header, if it helps, from most of my PHP scripts
is as follows:

<?php
require_once('Connections/cwab.php');
include ('test-include.php');

session_cache_limiter('nocache');
session_name ("cwautobodyphpsession");
session_start();
?>

Thanks for any insight as to what may be causing this.

--
- Michael J. Astrauskas

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Michael J. Astrauskas wrote:
I have a site where a user logs in and a session variable I created is
used to keep track of the fact that the user is logged in. This various
pages query a MySQL database to get information (mostly to generate a
catalogue and news page). Occasionally when loading a page I simply get
the error "No database selected" when the PHP script attempts to run the
query. Usually I can press reload in my browser (Netscape, IE, and
Opera) and the page will work. Sometimes it won't. What may be causing
this error? The header, if it helps, from most of my PHP scripts is as
follows:

<?php
require_once('Connections/cwab.php');
include ('test-include.php');

session_cache_limiter('nocache');
session_name ("cwautobodyphpsession");
session_start();
?>

Thanks for any insight as to what may be causing this.


I would guess that you have your database name stored as a session
variable, and that you use this to read/write to your tables. I would
guess that this 'interaction' is somewhere in cwab.php or
test-include.php - Because you've not supplied to code to these
functions, I cannot be sure, nor come up with a suggestion of a fix...
however...

As a work around, you could preceed the table name with the database
name using a period where ever you perform your
select/update/insert/whatever.

Thus if your database was called thetimes and your table was called
subscriptions, and you were looking for a firstname, you might have

SELECT firstname FROM subscriptions WHERE login='randelld' LIMIT 1;

If this was the case, I'd suggest you change it to

SELECT firstname FROM thetimes.subscriptions WHERE login='randelld' LIMIT 1;

Does that help any?
randelld
Jul 17 '05 #2

P: n/a
Reply-Via-Newsgroup Thanks wrote:
Michael J. Astrauskas wrote:
I have a site where a user logs in and a session variable I created
is used to keep track of the fact that the user is logged in. This
various pages query a MySQL database to get information (mostly to
generate a catalogue and news page). Occasionally when loading a page
I simply get the error "No database selected" when the PHP script
attempts to run the query. Usually I can press reload in my browser
(Netscape, IE, and Opera) and the page will work. Sometimes it won't.
What may be causing this error? The header, if it helps, from most of
my PHP scripts is as follows:

<?php
require_once('Connections/cwab.php');
include ('test-include.php');
session_cache_limiter('nocache');
session_name ("cwautobodyphpsession");
session_start();
?>

Thanks for any insight as to what may be causing this.


I would guess that you have your database name stored as a session
variable, and that you use this to read/write to your tables. I would
guess that this 'interaction' is somewhere in cwab.php or
test-include.php - Because you've not supplied to code to these
functions, I cannot be sure, nor come up with a suggestion of a fix...
however...

As a work around, you could preceed the table name with the database
name using a period where ever you perform your
select/update/insert/whatever.

Thus if your database was called thetimes and your table was called
subscriptions, and you were looking for a firstname, you might have

SELECT firstname FROM subscriptions WHERE login='randelld' LIMIT 1;

If this was the case, I'd suggest you change it to

SELECT firstname FROM thetimes.subscriptions WHERE login='randelld'
LIMIT 1;

Does that help any?
randelld


After playing with it for 15 minutes it seems to work... so far. I'll
give you an update later today if it works completely. Thank you so far!

cwab.php is a MySQL connection script written by Dreamweaver. It's as
follows (names changed to protect the innocent):

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_cwab = "localhost";
$database_cwab = "cwab_com";
$username_cwab = "blah";
$password_cwab = "blah";
$cwab = mysql_pconnect($hostname_cwab, $username_cwab,
$password_cwab) or die(mysql_error());
?>

I'm fairly certain it doesn't die at the above mysql_pconnect as the
page title appears *before* the error does, meaning a later MySQL call
had an error. I believe it's dying when I call mysql_query, but I don't
know why. And example call is below:

$parts = mysql_query($query_parts, $cwab) or die(mysql_error());

where $query_parts is some (tested) valid MySQL query.

Since the scope of require_once is that particular script, cwab.php
will be loaded every time my page is loaded for a single user, correct?

test-include.php is a series of functions I've written that don't
interact with the database or variables pointing to it at all. It's long
and uninteresting.

--
- Michael J. Astrauskas

Jul 17 '05 #3

P: n/a
Michael J. Astrauskas wrote:
Reply-Via-Newsgroup Thanks wrote:
Michael J. Astrauskas wrote:
I have a site where a user logs in and a session variable I created
is used to keep track of the fact that the user is logged in. This
various pages query a MySQL database to get information (mostly to
generate a catalogue and news page). Occasionally when loading a page
I simply get the error "No database selected" when the PHP script
attempts to run the query. Usually I can press reload in my browser
(Netscape, IE, and Opera) and the page will work. Sometimes it won't.
What may be causing this error? The header, if it helps, from most of
my PHP scripts is as follows:

<?php
require_once('Connections/cwab.php');
include ('test-include.php');
session_cache_limiter('nocache');
session_name ("cwautobodyphpsession");
session_start();
?>

Thanks for any insight as to what may be causing this.


I would guess that you have your database name stored as a session
variable, and that you use this to read/write to your tables. I would
guess that this 'interaction' is somewhere in cwab.php or
test-include.php - Because you've not supplied to code to these
functions, I cannot be sure, nor come up with a suggestion of a fix...
however...

As a work around, you could preceed the table name with the database
name using a period where ever you perform your
select/update/insert/whatever.

Thus if your database was called thetimes and your table was called
subscriptions, and you were looking for a firstname, you might have

SELECT firstname FROM subscriptions WHERE login='randelld' LIMIT 1;

If this was the case, I'd suggest you change it to

SELECT firstname FROM thetimes.subscriptions WHERE login='randelld'
LIMIT 1;

Does that help any?
randelld

After playing with it for 15 minutes it seems to work... so far. I'll
give you an update later today if it works completely. Thank you so far!

cwab.php is a MySQL connection script written by Dreamweaver. It's as
follows (names changed to protect the innocent):

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_cwab = "localhost";
$database_cwab = "cwab_com";
$username_cwab = "blah";
$password_cwab = "blah";
$cwab = mysql_pconnect($hostname_cwab, $username_cwab, $password_cwab)
or die(mysql_error());
?>

I'm fairly certain it doesn't die at the above mysql_pconnect as the
page title appears *before* the error does, meaning a later MySQL call
had an error. I believe it's dying when I call mysql_query, but I don't
know why. And example call is below:

$parts = mysql_query($query_parts, $cwab) or die(mysql_error());

where $query_parts is some (tested) valid MySQL query.

Since the scope of require_once is that particular script, cwab.php
will be loaded every time my page is loaded for a single user, correct?

test-include.php is a series of functions I've written that don't
interact with the database or variables pointing to it at all. It's long
and uninteresting.


Your 'require_once' will run for very one, every time - the "_once" and
the end of the require is useful only when its possible that you might
have duplicate includes/require statements (perhaps one included file
includes another which includes another which might include the second
include file again so require_once will only include the file once and
not shout/error... Note, while include and require differ, they have
similarities hence I hope you understand that I use require/include
interchangably though in programming circles, one might not.

Do you have a mysql error log that you can browse?

What about reading the output string from mysql_error() If MySQL was to
fail on you, the mysql servers message would be written there - that
might help you.

Alas, I won't be around until Tuesday - I don't have high speed at home
just yet (will do inside the next weeks) - its a long holiday weekend
here in the UK so I hope you get things resolved by then - If not, I'll
do my best to help if you email me using the address

website 2004 at fiprojects dot com

all of the above without spaces and whatever...

anyway... all the best,

laters
randell d
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.