Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Seperating Database Connection from Codes 1

Status
Not open for further replies.

namida

Programmer
May 29, 2003
101
AU
Hi,

I need to seperate my connection statements

<?
$db = mysql_connect(&quot;localhost&quot;, &quot;something&quot;,&quot;somethingelse&quot;);
mysql_select_db(&quot;something&quot;,$db);

?>

and all my other codes in another page. I want to do this because I don't want to change every page everytime I change the database password.

So I seperated the above code, and include it in all of my pages. The problem is, some pages worked, some pages don't. Although I included them the same way

using the normal
include(&quot;
It's not supposed to be directory problem, some pages that have the problems are in the same directory with connectme.php and some pages that doesn't work are not in the same directory.

Here's a code snippet that works

<?php
session_start();

if($_SESSION[&quot;loginid&quot;]){
$loginname=$_SESSION[&quot;loginid&quot;];

include (&quot;
$sql = &quot;Select * from msg where posttime ='$ts' and recipient='$loginname'&quot;;
$result = mysql_query($sql) or die (&quot;Could not execute query: $query. &quot; . mysql_error());
$mailrow= mysql_fetch_array($result);

Here's a code snippet that doesn't work

<?php
session_start();
if ($_SESSION['loginid'])
{
$loginname = $_SESSION['loginid'];
include (&quot;
$sql = &quot;Select * from set where loginname = '$loginname'&quot;;
$resultset = mysql_query($sql);


Does anyone has any idea? Or is there any better way to do it?

Regards,

Namida
 
Oh and what I mean by 'it doesn't work' is

it gives

Warning: mysql_query() [function.mysql-query]: Access denied for user: 'nobody@localhost' (Using password: NO)



Regards,

Namida
 
namida,

Look at the error message. It happens when a query is issued - not necessarily the query you show us in the snippet.

I suppose that you have a query in your code after the part for logged in users. When the quewry is issued there is no connection to the dbServer and MySQL attemps to connect with the default values, which is user 'nobody' at 'localhost' and no password.
And that, of course, fails.

I think the problem lies further down in your code, in the part we can't see.
 
Hi
Thanks for the reply
I tried moving the include to the very top.. before deciding if the user has logged in.
It's still not working.

The original code was :
<?php

session_start();


if ($_SESSION['loginid'])
{
$id = $_SESSION['loginid'];

$db = mysql_connect(&quot;localhost&quot;, &quot;something&quot;,&quot;something else&quot;);
mysql_select_db(&quot;something&quot;,$db);

$sql = &quot;Select * from set where loginname = '$loginname'&quot;;
$resultset = mysql_query($sql);

I tried this code and it also didn't work.

<?php

session_start();

$db = mysql_connect(&quot;localhost&quot;, &quot;something&quot;,&quot;something else&quot;);

mysql_select_db(&quot;something&quot;,$db);

if ($_SESSION['loginid'])
{
$id = $_SESSION['loginid'];



$sql = &quot;Select * from set where loginname = '$loginname'&quot;;
$resultset = mysql_query($sql);


I checked the part after the login and there is no queries.. just error messages saying they need to login.



Regards,

Namida
 
Let's make 100% sure that the connection is in fact established:
Code:
$db = mysql_connect(&quot;localhost&quot;, &quot;something&quot;,&quot;something else&quot;) OR die('Unable to connect. MySQL said: mysql_error());

Let's go from there.
 
Clicked too fast...
Code:
$db = mysql_connect(&quot;localhost&quot;, &quot;something&quot;,&quot;something else&quot;) OR die(&quot;Unable to connect. MySQL said:&quot;. mysql_error());
 
Hi, thanks.

Didn't die.
I tried putting a $a = 4 in the connectme.php and printing it out in the included page.

Looks like the variables wasn't passed through.... :(

But if I print it out directly in the connectme.php

eg.

below the connect line I had put
echo &quot;I'm here&quot;;
and
it's printed out.

*confused*


Regards,

Namida
 
What does your include line look like? Do you actually have the URL as you do in the first post? If so, that's your problem. You should include the file with the local path, not a URL unless the URL prints PHP code (for your purpose, this just ruins security).

//Daniel
 
Local as in the full local path from the file server root, not local in the sense of the web server's document root.

Example:
/usr/var/apache/
or if it's within a home:
/home/username/public_html/..
 
Wow thanks that works!

But why did some of the pages work and some don't.. Hmm

anyway thanks!

Regards,

Namida
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top