PHP/SQL error with includes, wtf?
I'm a PHP developper and I work for a webhost and am calling out for help. :(
I need someone to give me a hand.
I have a script called config.php, it contains:
-=-=-
// Important paths.
$db_include = "/home/kl/kl_db.inc";
// DB information...
$kldb["username"] = "username_here";
$kldb["password"] = "password_here";
$kldb["db"] = "database_here";
$active_status = 5;
-=-=-
I have a functions.php file that contains:
-=-=-
function GetActiveUsers() {
global $kldb,$active_status,$db_include;
include $db_include;
$dbusername = $kldb["username"];
$dbuserpassword = $kldb["password"];
$default_dbname = $kldb["db"];
error_reporting(0);
$link_id = db_connect();
if(!$link_id) {
$activeusers["error"] = sql_error();
}
else {
$query = "SELECT Name FROM affiliates WHERE StatusId = $active_status";
$execute = mysql_query($query);
$i = 0;
while ($rows = mysql_fetch_rows($execute)) {
$activeusers[$i] = $rows[0];
$i++;
}
}
mysql_close($link_id);
return $activeusers;
}
?>
-=-=-
When I run my program called splits.php, it doesn't connect to my SQL DB... here's splits.php:
#!/usr/local/bin/php -q
Now, the problem is, if I use my database object (kl_db.inc) standalone, without inclusion, it works. If I implement this and echo out the vars within the function, I get the right echo outs but the program returns this on runtime:
The program has quit because: 1046: No Database Selected
I'm on the brink of insanity here as I can't figure out what I'm doing wrong. I pass my objects, and all that and it won't friggen connect when I call the damned GetActiveUsers() function. I don't understand. If I put all this in the splits.php file, it works.
I know the db object works... here it is as well (kl_db.inc):
-=-=-
//common_db.inc
$dbhost = 'localhost';
$dbusername = 'username_here';
$dbuserpassword = 'password_here';
$default_dbname = 'database_here';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
function db_connect($dbname='') {
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if(!$link_id) {
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host $dbhost.";
return 0;
}
else if(empty($dbname) && !mysql_select_db($default_dbname)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else if(!empty($dbname) && !mysql_select_db($dbname)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else return $link_id;
}
function sql_error() {
global $MYSQL_ERRNO, $MYSQL_ERROR;
if(empty($MYSQL_ERROR)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
-=-=-
Please help!!!!!
Like I said within the cut, the db object works because I'm using this exact same one with the same username/password and all that on this same server. The problem is that when I include my object and connect in the function, it does NOT connect to the database, but burps up:
The program has quit because: 1046: No Database Selected
Helphelphelphelphelp... :P
Jamie
I need someone to give me a hand.
I have a script called config.php, it contains:
-=-=-
// Important paths.
$db_include = "/home/kl/kl_db.inc";
// DB information...
$kldb["username"] = "username_here";
$kldb["password"] = "password_here";
$kldb["db"] = "database_here";
$active_status = 5;
-=-=-
I have a functions.php file that contains:
-=-=-
function GetActiveUsers() {
global $kldb,$active_status,$db_include;
include $db_include;
$dbusername = $kldb["username"];
$dbuserpassword = $kldb["password"];
$default_dbname = $kldb["db"];
error_reporting(0);
$link_id = db_connect();
if(!$link_id) {
$activeusers["error"] = sql_error();
}
else {
$query = "SELECT Name FROM affiliates WHERE StatusId = $active_status";
$execute = mysql_query($query);
$i = 0;
while ($rows = mysql_fetch_rows($execute)) {
$activeusers[$i] = $rows[0];
$i++;
}
}
mysql_close($link_id);
return $activeusers;
}
?>
-=-=-
When I run my program called splits.php, it doesn't connect to my SQL DB... here's splits.php:
#!/usr/local/bin/php -q
Now, the problem is, if I use my database object (kl_db.inc) standalone, without inclusion, it works. If I implement this and echo out the vars within the function, I get the right echo outs but the program returns this on runtime:
The program has quit because: 1046: No Database Selected
I'm on the brink of insanity here as I can't figure out what I'm doing wrong. I pass my objects, and all that and it won't friggen connect when I call the damned GetActiveUsers() function. I don't understand. If I put all this in the splits.php file, it works.
I know the db object works... here it is as well (kl_db.inc):
-=-=-
//common_db.inc
$dbhost = 'localhost';
$dbusername = 'username_here';
$dbuserpassword = 'password_here';
$default_dbname = 'database_here';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
function db_connect($dbname='') {
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if(!$link_id) {
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host $dbhost.";
return 0;
}
else if(empty($dbname) && !mysql_select_db($default_dbname)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else if(!empty($dbname) && !mysql_select_db($dbname)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else return $link_id;
}
function sql_error() {
global $MYSQL_ERRNO, $MYSQL_ERROR;
if(empty($MYSQL_ERROR)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
-=-=-
Please help!!!!!
Like I said within the cut, the db object works because I'm using this exact same one with the same username/password and all that on this same server. The problem is that when I include my object and connect in the function, it does NOT connect to the database, but burps up:
The program has quit because: 1046: No Database Selected
Helphelphelphelphelp... :P
Jamie
