Help with require() and variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jvdc
Forum Newbie
Posts: 5
Joined: Sat Jul 14, 2007 2:49 pm

Help with require() and variables

Post by jvdc »

Hi!

I have the following problem:
I am including two files, one of which is a DB link, the other is the DB configuration. The DB link variable somehow never gets recognized though.

The usernames and passwords are correct. When I try to run the db_connect() function, though, I get this error message:

Code: Select all

Warning: mysql_connect(): Access denied for user: 'www-data@localhost' (Using password: NO) in /www/htdocs/ludo/include/db_functions.php on line 7
Could not connect: Access denied for user: 'www-data@localhost' (Using password: NO)
It seems to me that the include doesn't work correctly.

If I take the mysql_connect() function out of the db_connect() function, the connection works, but somehow the $db variable is never recognized: When db_disconnect() is called, it tells me that it's not a valid database link.

What am I doing wrong? Am I missing something about require/include?

/index.php

Code: Select all

<?php 
require("include/db_functions.php");
db_connect();
db_disconnect();
?>
/include/config.php

Code: Select all

<?php 
global $db_host;
global $db_name;
global $db_user;
global $db_pass;
global $db_lock;

// Database settings
$db_host        = "localhost";
$db_name        = "dbname";
$db_user        = "username";
$db_pass        = "password";

// Locking Changes
$db_lock        = FALSE;
?>
/include/db_functions.php

Code: Select all

<?php
require_once("config.php");
global $db;

function db_connect() {

    $db = mysql_connect($db_host, $db_user, $db_pass)
	    or die ("Could not connect: " . mysql_error());

    mysql_select_db($db_name) or die ("Could not select database {$db_name}!");

}

function db_disconnect() {
    
    mysql_close($db);
       
}
?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

It says "Using Password: NO," which means that it's not recognizing the password at all. No idea why, but that's what it means. It's saying you are attempting to log in without using a password. Try echoing all of the variables from within the db_connect function to ensure they all have the correct values.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<?php
function db_connect() {
  global $db;
  require 'config.php';
  $db = mysql_connect($db_host, $db_user, $db_pass)
    or die ("Could not connect: " . mysql_error());
  mysql_select_db($db_name) or die ("Could not select database {$db_name}!");

}

function db_disconnect() {
  global $db;
  mysql_close($db);     
}
?>
and remove the global xyz; lines from config.php
jvdc
Forum Newbie
Posts: 5
Joined: Sat Jul 14, 2007 2:49 pm

Post by jvdc »

I tried both suggestions.

When I echo the database config variables in the db_connect() function, it just gives me nothing.

When I re-declare $db as a global variable, nothing changes either.

Is there any reason why a require() statement might not be evaluated correctly?
jvdc
Forum Newbie
Posts: 5
Joined: Sat Jul 14, 2007 2:49 pm

Post by jvdc »

Addendum:

If I pull the mysql_connect() function out of the db_connect() function (directly into db_functions.php), the connection works. Any further reference to $db gives me errors like this one, though:

Code: Select all

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /www/htdocs/ludo/include/db_functions.php on line 66
It seems like no variable declared outside a function is available within a function. I just don't get it!
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

It seems like no variable declared outside a function is available within a function.
1) to determine this, replace the innards of db_connect with

Code: Select all

echo "Password: ".$db_pass."<br>";
echo "Host: ".$db_host."<br>";
echo "Username: ".$db_user."<br>";
echo "DB Name: ".$db_name."<br>";
That is what superdezign meant by
superdezign wrote:echoing all of the variables from within the db_connect function to ensure they all have the correct values.
2) If I'm not mistaken (I'm a little rusty here), declaring variables global in the global scope does nothing. You need to declare "global $x" in the scope that you are trying to assign a value to.
For example:

Code: Select all

$x=42;
function y(){
  $x=1;
  echo $x;
}
function z(){
  global $x;
  echo $x;
}
echo $x;//Prints 42
echo y();//Prints 1
echo z();//Prints 42
If y() was assigning to the global scoped $x, z() would have returned 1.
If you added "global $x" to the beginning of y(), y() would have changed the original $x to 1.
Do you understand? or am I confusing?
Post Reply