sniperman
12-13-2010, 09:39 AM
Hi all, very confused as to why this code does not function when a simple database connection works.
This is the code that calls all the configuration.
// Load cfg
require_once(DATA . 'config.php');
// Load DB
require_once(DATA . 'dbconfig.php');
require_once(INC . 'mysql.php');
$DB = new MySQL();
$DB->server = DBHOST;
$DB->user = DBUSER;
$DB->password = DBPASS;
$DB->database = DBNAME;
$DB->connect();
This is inside the dbconfig.php file
<?PHP
define ("DBHOST", "localhost");
define ("DBNAME", "dbase_dbase");
define ("DBUSER", "dbase_user");
define ("DBPASS", "==password");
?>
This is the mysql.php string.
<?php
if(!defined('IN_MY'))
die("Oops, I think you want to hack me!");
// SQL Save function
function SQLSave($theValue, $theType)
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ?
mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType)
{
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
// ##################### MySQL Connection Handle Library #######################
class MySQL
{
var $server = "localhost";
var $user = "root";
var $password = "";
var $database = "";
var $conn = 0;
var $queryid = 0;
var $row = array();
var $errdesc = "";
var $errno = 0;
// ###################### connect #######################
function connect()
{
if(0 == $this->conn)
{
if($this->password=="")
{
$this->conn = mysql_connect($this->server.":3306",$this->user);
}
else
{
$this->conn = mysql_connect($this->server.":3306",$this->user,$this->password);
}
if(!$this->conn)
{
$this->error("Connection == false, connect failed");
}
if($this->database != "")
{
if(!mysql_select_db($this->database, $this->conn))
{
$this->error("cannot use database ".$this->database);
}
}
}
}
The error spots are marked in red. For some reason it fails there and submits the fail error. Which is odd because when I use the simple database connector below, it always connects to the database.
// Load DB
$link = mysql_connect('localhost', 'dbase_user', '==password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('dbase_dbase', $link);
if (!$db_selected) {
die('<p>unable to locate the database</p>');
}
This is the code that calls all the configuration.
// Load cfg
require_once(DATA . 'config.php');
// Load DB
require_once(DATA . 'dbconfig.php');
require_once(INC . 'mysql.php');
$DB = new MySQL();
$DB->server = DBHOST;
$DB->user = DBUSER;
$DB->password = DBPASS;
$DB->database = DBNAME;
$DB->connect();
This is inside the dbconfig.php file
<?PHP
define ("DBHOST", "localhost");
define ("DBNAME", "dbase_dbase");
define ("DBUSER", "dbase_user");
define ("DBPASS", "==password");
?>
This is the mysql.php string.
<?php
if(!defined('IN_MY'))
die("Oops, I think you want to hack me!");
// SQL Save function
function SQLSave($theValue, $theType)
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ?
mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType)
{
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
// ##################### MySQL Connection Handle Library #######################
class MySQL
{
var $server = "localhost";
var $user = "root";
var $password = "";
var $database = "";
var $conn = 0;
var $queryid = 0;
var $row = array();
var $errdesc = "";
var $errno = 0;
// ###################### connect #######################
function connect()
{
if(0 == $this->conn)
{
if($this->password=="")
{
$this->conn = mysql_connect($this->server.":3306",$this->user);
}
else
{
$this->conn = mysql_connect($this->server.":3306",$this->user,$this->password);
}
if(!$this->conn)
{
$this->error("Connection == false, connect failed");
}
if($this->database != "")
{
if(!mysql_select_db($this->database, $this->conn))
{
$this->error("cannot use database ".$this->database);
}
}
}
}
The error spots are marked in red. For some reason it fails there and submits the fail error. Which is odd because when I use the simple database connector below, it always connects to the database.
// Load DB
$link = mysql_connect('localhost', 'dbase_user', '==password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('dbase_dbase', $link);
if (!$db_selected) {
die('<p>unable to locate the database</p>');
}