Jump to content

[SOLVED]Problem with my DB class


Sasuun

Recommended Posts

I'm getting the following error whenever I try to execute a query using my db class.
[code]No Database Selected
pwned[/code]

I have no idea why this is happening because I know for a fact a database is being selected and I've tried seeing if there were any connection errors, but nothing worked. I'm hoping someone else's input can help me solve this problem

[b]db_mysql.php[/b][code]
class db_mysql
{
public $connection = '';
protected $db = '';
public $query_id = '';
public $cur_query  = '';
public $error = '';
public $query_count = '0';
public $row = '';
function __construct()
{
global $INFO;
//------------------------------------------------
// do we want to make a persistant db connection
//------------------------------------------------
$this->connection = mysql_connect($host,$user,$pass);
mysql_select_db($db,$this->connection);
//------------------------------------------------
// setup our sql prefix
//------------------------------------------------
if ( ! defined( 'SQL_PREFIX' ))
{
define ( 'SQL_PREFIX', $INFO['dbprefix']);
}
}
public function dbconnect($host,$user,$pass,$db)
{
$this->connection = mysql_connect($host,$user,$pass);
mysql_select_db($db,$this->connection);

}
public function do_query( $query = '')
{
if ($query == '')
{
$query = $this->cur_query;
}

if ( SQL_PREFIX != "xms_" )
{
$query = preg_replace("/\sxms_(\S+?)([\s\.,]|$)/", " ".SQL_PREFIX."\\1\\2", $query);
}
$this->query_id = mysql_query($query)
or die(mysql_error() . "<br />pwned");
echo $this->query_id;
$this->query_count++;

}
public function fetch_row( $ci = '')
{
if ( $ci == '')
{
$ci = $this->query_id;
}
$this->row = mysql_fetch_array( $ci );

return $this->row;
$this->row = '';
}
public function simple_construct( $q )
{
if ( $q['SELECT'] )
{
$this->construct_select( $q['SELECT'], $q['FROM'], &$q['WHERE'] );
}
elseif ( $q['UPDATE'])
{
$this->construct_update( $q['UPDATE'], $q['SET'], &$q['WHERE'] );
}
elseif ( $q['DELETE'])
{
$this->construct_delete( $q['DELETE'], $q['WHERE'] );
}
elseif ( $q['INSERT'])
{
$this->construct_insert( $q['INSERT'],  $q['FIELDS'], $q['VALUES']);
}
if ( @$q['ORDER'] )
{
$this->construct_order( &$q['ORDER'] );
}
if ( $q['LIMIT'])
{
$this->construct_limit( &$q['LIMIT']);

}
if ( !$q)
{
$this->cur_query = '';
}

if ($this->cur_query != '')
{
$this->do_query();

$this->cur_query = '';
}
}
public function construct_insert( $insert, $fields, $values )
{
$this->cur_query = "INSERT INTO $insert
($fields)
VALUES($values)";
}
public function construct_select( $select, $from, $where = "" )
{
$this->cur_query = "SELECT " . $select . " FROM " . SQL_PREFIX . $from;
if ($where != "")
{
$this->cur_query .= "WHERE " . $where;
}
}
public function construct_update( $update, $set, $where)
{
$this->cur_query = "UPDATE " . SQL_PREFIX . $update . "SET " . $set;
if ( $where )
{
$this->cur_query .= "WHERE " . $where;
}
}
public function construct_delete( $delete, $where)
{
$this->cur_query = "DELETE FROM " . SQL_PREFIX . $delete;
if ( $where )
{
$this->cur_query .= "WHERE $where";
}
}
public function construct_order( $order )
{
$this->cur_query .= "ORDER BY $order";
}
public function construct_limit( $limit )
{
$this->cur_query .= " LIMIT $limit";
}
public function num_rows( $ci = '' )
{
if ( $ci = '' )
{
$ci = $this->query_id;
}
return mysql_num_rows( $ci );
}
public function get_query_count()
{
return $this->query_count;
}
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/18845-solvedproblem-with-my-db-class/
Share on other sites

You have the following:

function __construct()
{
...
$this->connection = mysql_connect($host,$user,$pass);
mysql_select_db($db,$this->connection
...
}

How does the constructer get the variables? shouldn't you have something like __construct($var='something', $var2='something') { ... }

Sorry if i got it wrong.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.