...

View Full Version : OOP database objects



jfreak53
11-13-2011, 03:57 PM
Ok so I have an SQL class that I made myself and has worked for me so far. I am now trying to use two SEPARATE connections and it seems to be failing badly and I don't know why.

Shouldn't I be able to create two new objects each with it's own DB connection and each one maintain as an object? Using PHP5 by the way.

The SQL class is:


class sql {

static $query;
static $return;
static $numrows;
static $result;
static $db;

function __construct ( $dbhost, $dbuser, $dbpass, $dbname ) {
if (is_object($this)) {
$this->query = '';
$this->return = '';
$this->numrows = 0;
$this->result = '';
$this->db = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Cannot connect to database.');
mysql_select_db($dbname, $this->db);
} else {
self::$query = '';
self::$return = '';
self::$numrows = 0;
self::$result = '';
self::$db = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Cannot connect to database.');
mysql_select_db($dbname, self::$db);
}
}

function close () {
if (is_object($this)) {
mysql_close($this->db);
} else {
mysql_close(self::$db);
}
}

// === Function to log MySQL problems ===
function logger ( $qry, $err = '' ) {

$qry = addslashes($qry);
$err = addslashes($err);
makeLog("Error in MySQL query. The Query was: \"$qry\". The error MySQL gave was: $err");

}

function error ( $qry, $err, $ret ) {

if (is_object($this)) {
$this->logger($qry, $err);
} else {
self::logger($qry, $err);
}

if($ret == 1) {
die('Error in SQL: '.$err.' - Query: '.$qry);
} else if ($ret == 2) {
echo 'Transaction Error';
}

}

function update ( $qry, $ret = 1 ) {
global $db, $dbg, $debug;

$dbg[] = $qry;
$return = mysql_query($qry);
if(!$return) {
if (is_object($this)) {
$this->error($qry, mysql_error(), $ret);
} else {
self::error($qry, mysql_error(), $ret);
}
} else {
if($ret == 3) {
echo '<font color="green">Successful Transaction..</font><br>';
}
}

}

function updateA ( $data, $items, $where = '' ) {
global $db, $dbg, $debug;

if(is_array($items)) {
foreach($items as $key => $value) {
$ups[] = $key." = '".sanSQL($value)."'";
}

$ups = implode(', ', $ups);

$qry = "UPDATE $data SET $ups $where";

if (is_object($this)) {
$this->insert($qry);
} else {
self::insert($qry);
}
} else {
echo 'We need the data fields to insert';
}

}

function insert ( $qry, $ret = 1 ) {
global $db, $dbg, $debug;

$dbg[] = $qry;
$return = mysql_query($qry);
if(!$return) {
if (is_object($this)) {
$this->error($qry, mysql_error(), $ret);
} else {
self::error($qry, mysql_error(), $ret);
}
} else {
if($ret == 3) {
echo '<font color="green">Successful Transaction..</font><br>';
}
}

}

function insertA ( $data, $items ) {
global $db, $dbg, $debug;

if(is_array($items)) {
foreach($items as $key => $value) {
$rows[] = $key;
$vals[] = "'".sanSQL($value)."'";
}

$rows = implode(', ', $rows);
$vals = implode(', ', $vals);

$qry = "INSERT INTO $data ($rows) VALUES ($vals)";

if (is_object($this)) {
$this->insert($qry);
} else {
self::insert($qry);
}
} else {
echo 'We need the data fields to insert';
}

}

function remove ( $qry, $ret = 1 ) {
global $db, $dbg, $debug;

$dbg[] = $qry;
$return = mysql_query($qry);
if(!$return) {
if (is_object($this)) {
$this->error($qry, mysql_error(), $ret);
} else {
self::error($qry, mysql_error(), $ret);
}
} else {
if($ret == 3) {
echo '<font color="green">Successful Transaction..</font><br>';
}
}

}

function singleSel ( $qry, $type = 1, $ret = 1 ) {
global $db, $dbg, $debug;

$dbg[] = $qry;
$final = @mysql_query($qry);
if(@mysql_num_rows($final) > 0) {
$return = @mysql_result($final,0,0);
} else {
$return = '';
}

if(!$return && $return < '') {
if (is_object($this)) {
$this->error($qry, mysql_error(), $ret);
} else {
self::error($qry, mysql_error(), $ret);
}
} else {
if($type == 1) {
return $return;
} else if($type == 2) {
echo $return;
}
}

}

function rowSelect ( $qry, $type = 1, $ret = 1 ) {
global $db, $dbg, $debug;

$dbg[] = $qry;
$final = mysql_query($qry);
if(mysql_num_rows($final) > 0) {
$return = mysql_fetch_array($final);
} else {
$return = '';
}

if(!$return && $return < '') {
if (is_object($this)) {
$this->error($qry, mysql_error(), $ret);
} else {
self::error($qry, mysql_error(), $ret);
}
} else {
if($type == 1) {
return $return;
} else if($type == 2) {
echo implode(';', $return);
}
}

}

function rowSelectRet ( $qry, $ret = 1 ) {
global $db, $dbg, $debug;

$dbg[] = $qry;
$return = mysql_query($qry);
if(!$return) {
if (is_object($this)) {
$this->error($qry, mysql_error(), $ret);
} else {
self::error($qry, mysql_error(), $ret);
}
} else {

if (is_object($this)) {
$this->numrows = mysql_num_rows($return);
} else {
self::$numrows = mysql_num_rows($return);
}

while($row = mysql_fetch_array($return)) {
$result[] = $row;
}

return $result;
}

}

function getRows () {

if (is_object($this)) {
return $this->numrows;
} else {
return self::$numrows;
}

}

function getLastId () {

return mysql_insert_id();

}


}

Like I said it ALL works fine, the problem is now when I try to create two separate connections to two different DBs. For some reason after the second db is connected and a new object made for it any call to DB1 works off of DB2. Let me show a bit of code.


$db1 = new sql($dbhost, $dbuser, $dbpass, $dbname);
$dbs = $db1->rowSelect("SELECT * FROM table WHERE id = {$_POST[id]}");

That works fine, then DB2:


$db1 = new sql($dbhost, $dbuser, $dbpass, $dbname);
$db2 = new sql($dbs[5], $dbs[6], $dbs[7], $dbs[4]);
$dbs = $db1->rowSelect("SELECT * FROM table WHERE id = {$_POST[id]}");

This gives me an error, it can't find the table "table" under the DB for con2. So for some reason even though I am referencing $db1 in my query it is still trying to connect using $db2.

Any ideas?

kbluhm
11-13-2011, 04:30 PM
So much is wrong.

$this will always be an object.

You are declaring properties as static (static $db;), but then referencing them as both static (self::$db) as well as non-static ($this->db).

And I haven't even gotten to the debugging yet. ;)

If you're not seeing like 100 notices being thrown, you must be using PHP4, have all errors suppressed, or both.

To allow multiple connections, drop all the static stuff.

jfreak53
11-13-2011, 04:39 PM
That is because during my code I use them as sql::function more than I use them as an object. Hence why I check if it's an object or not before I call it.

I'm actually not getting any error's at all, I am using PHP5 and I have E_ALL in error's turned on since it's my Dev environment. I have been using this code for 3 or 4 system's now with out problems.

If I don't declare var's as static, what do I declare them as?

kbluhm
11-13-2011, 04:46 PM
You cannot (should not) have it both ways. If you declare a function as public, you should not access it as static... and vice versa.

I just copied the class locally, tried connecting to a local DB, and here is my result:


error_reporting( E_ALL | E_STRICT );
ini_set( 'display_errors', 1 );

/* sql class code */

$sql = new sql( 'localhost', 'root', '', 'kbluhm_local' );

Output:



<br />
<b>Strict Standards</b>: Accessing static property sql::$query as non static in <b>C:\WAMP\www\local.cobaltdev.org\public_html\index.php</b> on line <b>16</b><br />
<br />
<b>Strict Standards</b>: Accessing static property sql::$return as non static in <b>C:\WAMP\www\local.cobaltdev.org\public_html\index.php</b> on line <b>17</b><br />
<br />
<b>Strict Standards</b>: Accessing static property sql::$numrows as non static in <b>C:\WAMP\www\local.cobaltdev.org\public_html\index.php</b> on line <b>18</b><br />
<br />
<b>Strict Standards</b>: Accessing static property sql::$result as non static in <b>C:\WAMP\www\local.cobaltdev.org\public_html\index.php</b> on line <b>19</b><br />
<br />
<b>Strict Standards</b>: Accessing static property sql::$db as non static in <b>C:\WAMP\www\local.cobaltdev.org\public_html\index.php</b> on line <b>20</b><br />
<br />
<b>Strict Standards</b>: Accessing static property sql::$db as non static in <b>C:\WAMP\www\local.cobaltdev.org\public_html\index.php</b> on line <b>21</b><br />

jfreak53
11-13-2011, 04:50 PM
Hmm, well on my end I have zero warnings, so not sure. At any rate, what do I change the $db variable to then to get it to keep separate connections? I have tried both private and protected and it still has the same problem.

kbluhm
11-13-2011, 05:14 PM
I'm not going to rewrite your entire class for you, but here is an example to get you pointed in the right direction. I just whipped this up, so typos may exist, but they shouldn't be major.


class sql
{

private $db;

public function __construct( $host, $user, $pass, $name )
{

if ( ! $this->db = mysql_connect( $host, $user, $pass ) )
{
throw new Exception(
'Unable to connect to ' . $host
);
}

if ( ! mysql_select_db( $dbname, $this->db ) )
{
throw new Exception(
'Unable to select ' . $name
);
}

}

public function __destruct()
{
$this->close();
}

public function close()
{
return mysql_close( $this->db );
}

public function query( $query )
{
return new sql_result( $query, $this->db );
}

}

class sql_result
{

private $result, $rowCount = 0;

public function __construct( $query, sql $db )
{
$this->result = mysql_query( $query, $db );
$this->rowCount = (
0 === stripos( trim( $query ), 'SELECT' ) ||
0 === stripos( trim( $query ), 'SHOW' )
) ? mysql_num_rows( $this->result ) : mysql_affected_rows( $db );
}

public function free()
{
return mysql_free_result( $this->result );
}

public function rowCount()
{
return $this->rowCount;
}

public function rewind()
{
return $this->seek( 0 );
}

public function seek( $i = 0 )
{
return mysql_data_seek( $this->result, $i );
}

public function fetch()
{
return mysql_fetch_assoc( $this->result );
}

public function fetchAll()
{

$this->rewind();
$rows = array();

while ( $row = $this->fetch() )
{
$rows[] = $row;
}

return $rows;

}

}


Notice two classes: sql and sql_result. It bugs me somewhat when people will "abstract" the connection, but not the result. :)


$sql = new sql( 'localhost', 'root', '', 'test_db' );

$res = $sql->query( '
SELECT *
FROM `test_table`
WHERE `test_datetime` > NOW()
LIMIT 10
' );

if ( ! $res->rowCount() )
{
echo 'Nothing found!';
}

// then
foreach ( $res->fetchAll() as $row )
{
var_dump( $row );
}

// or
while ( $row = $res->fetch() )
{
var_dump( $row );
}



In the above code, I would never access sql::query(); because it has been declared as non-static. To gracefully handle the PHP4-era syntax in your original code, PHP5 made itself backward-compatible in that not declaring static assumes public, as well as not declaring protected/private assumes public. So defining a method like so:


function foo( $bar )
{
return $bar;
}

...is identical to:


public function foo( $bar )
{
return $bar;
}

...which should not be called statically.

jfreak53
11-13-2011, 05:33 PM
Thank you, but I really wasn't asking anyone to re-create my class for me, as you can see by my original post I was asking where I could fix the multiple DB problem in the current class. I like my class, and it works for me and my use, but thank you once more, I don't think I'll be switching.

To be honest the class you posted added 3 extra steps for me that are not needed and can be handled class side, while with mine it's quick and simple and returns the array immediately when I call my row function, hence WHY I have multiple functions for each thing. But thanks.

I have changed the $db variable to private and contruct to public, but still have the same problems in my class. I have also in the rowSelectRet function changed mysql_query to:


mysql_query($qry, $this->db);

But still same problem. As I understand OOP, each time I create a NEW object it should all maintain a NEW Object. So why are the two mixing themselves?

Also when I try your Destruct function I get this error:


Warning: mysql_close(): 5 is not a valid MySQL-Link resource in libraries\sql.inc.php on line 26

kbluhm
11-13-2011, 05:40 PM
I'm not going to rewrite your entire class for you, but here is an example to get you pointed in the right direction. I just whipped this up, so typos may exist, but they shouldn't be major.

;)

I didn't say I was rewriting yours. I did, however, say I did not want to rewrite yours, and was providing an untested example to serve as more of a demo in proper syntax.

If you're trying to make multiple connections, and it's not allowing it... then something is in fact not working fine. Were I to take on a project with this class, the first thing I would do is refactor it from scratch.

You cannot have it both ways. Either the method is static or it is not. You should not be calling (for example!) $sql->query() in one place, and sql::query() in another without making complex usage of the __call() and __callStatic() magic methods... but we're not even going to get into that now because that type of usage would be entirely ridiculous. :D


As I understand OOP, each time I create a NEW object it should all maintain a NEW Object. So why are the two mixing themselves?
I'm not so sure you fully understand OOP as far as static properties and methods go. The two are being mixed because you are seemingly randomly interpolating static and non-static method and property calls, as well as declaring properties as static that evidently should not be. There is just so much wrong with that code, I contemplated just moving on and not saying anything at all.

jfreak53
11-13-2011, 05:48 PM
Ok, for the sake of argument and I need to find out what is wrong here ha ha

I removed all self:: instances and removed everything else like that. Now pure OOP, still doesn't work :(

This is my new code:


class sql {

private $query;
private $return;
private $numrows;
private $result;
private $db;

public function __construct ( $dbhost, $dbuser, $dbpass, $dbname ) {
$this->query = '';
$this->return = '';
$this->numrows = 0;
$this->result = '';
$this->db = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Cannot connect to database.');
mysql_select_db($dbname, $this->db);
}

/*public function __destruct() {
$this->close();
}*/

function close () {
mysql_close($this->db);
}

// === Function to log MySQL problems ===
function logger ( $qry, $err = '' ) {

$qry = addslashes($qry);
$err = addslashes($err);
makeLog("Error in MySQL query. The Query was: \"$qry\". The error MySQL gave was: $err");

}

function error ( $qry, $err, $ret ) {

$this->logger($qry, $err);

if($ret == 1) {
die('Error in SQL: '.$err.' - Query: '.$qry);
} else if ($ret == 2) {
echo 'Transaction Error';
}

}

function update ( $qry, $ret = 1 ) {
global $db, $dbg, $debug;

$dbg[] = $qry;
$return = mysql_query($qry);
if(!$return) {
$this->error($qry, mysql_error(), $ret);
} else {
if($ret == 3) {
echo '<font color="green">Successful Transaction..</font><br>';
}
}

}

function updateA ( $data, $items, $where = '' ) {
global $db, $dbg, $debug;

if(is_array($items)) {
foreach($items as $key => $value) {
$ups[] = $key." = '".sanSQL($value)."'";
}

$ups = implode(', ', $ups);

$qry = "UPDATE $data SET $ups $where";

$this->insert($qry);
} else {
echo 'We need the data fields to insert';
}

}

function insert ( $qry, $ret = 1 ) {
global $db, $dbg, $debug;

$dbg[] = $qry;
$return = mysql_query($qry);
if(!$return) {
$this->error($qry, mysql_error(), $ret);
} else {
if($ret == 3) {
echo '<font color="green">Successful Transaction..</font><br>';
}
}

}

function insertA ( $data, $items ) {
global $db, $dbg, $debug;

if(is_array($items)) {
foreach($items as $key => $value) {
$rows[] = $key;
$vals[] = "'".sanSQL($value)."'";
}

$rows = implode(', ', $rows);
$vals = implode(', ', $vals);

$qry = "INSERT INTO $data ($rows) VALUES ($vals)";

$this->insert($qry);
} else {
echo 'We need the data fields to insert';
}

}

function remove ( $qry, $ret = 1 ) {
global $db, $dbg, $debug;

$dbg[] = $qry;
$return = mysql_query($qry);
if(!$return) {
$this->error($qry, mysql_error(), $ret);
} else {
if($ret == 3) {
echo '<font color="green">Successful Transaction..</font><br>';
}
}

}

function singleSel ( $qry, $type = 1, $ret = 1 ) {
global $db, $dbg, $debug;

$dbg[] = $qry;
$final = @mysql_query($qry);
if(@mysql_num_rows($final) > 0) {
$return = @mysql_result($final,0,0);
} else {
$return = '';
}

if(!$return && $return < '') {
$this->error($qry, mysql_error(), $ret);
} else {
if($type == 1) {
return $return;
} else if($type == 2) {
echo $return;
}
}

}

function rowSelect ( $qry, $type = 1, $ret = 1 ) {
global $db, $dbg, $debug;

$dbg[] = $qry;
$final = mysql_query($qry);
if(mysql_num_rows($final) > 0) {
$return = mysql_fetch_array($final);
} else {
$return = '';
}

if(!$return && $return < '') {
$this->error($qry, mysql_error(), $ret);
} else {
if($type == 1) {
return $return;
} else if($type == 2) {
echo implode(';', $return);
}
}

}

function rowSelectRet ( $qry, $ret = 1 ) {
global $db, $dbg, $debug;

$dbg[] = $qry;
$return = mysql_query($qry, $this->db);
if(!$return) {
$this->error($qry, mysql_error(), $ret);
} else {

$this->numrows = mysql_num_rows($return);

while($row = mysql_fetch_array($return)) {
$result[] = $row;
}

return $result;
}

}

function getRows () {

return $this->numrows;

}

function getLastId () {

return mysql_insert_id();

}


}

Ok, now in theory I am still using OOP, this SHOULD work since it uses the same connection structure as your's right? I see it does, private and public. So the $db should be private and publicized.

This still doesn't work:


$db1 = new sql($dbhost, $dbuser, $dbpass, $dbname);
$db2 = new sql($dbs[5], $dbs[6], $dbs[7], $dbs[4]);
$dbs = $db1->rowSelect("SELECT * FROM table WHERE id = {$_POST[id]}");

In theory, when I private the code it should work right??

Let's say DB1 is "code" and DB2 is "tables". This is the error I get still:


Error in SQL: Table 'tables.table' doesn't exist - Query: SELECT id, name FROM table

And it should be "code.table". So as you can see DB1 is now using DB2, which doesn't make any sense at all since it's a new Object?

Leaving the coding styles aside and the fact that we both do things differently, this should work now.

jfreak53
11-13-2011, 05:49 PM
Ok, now in theory I am still using OOP, this SHOULD work since it uses the same connection structure as your's right? I see it does, private and public. So the $db should be private and publicized.

Sorry I meant to say "private and NOT publicized" ha ha

jfreak53
11-13-2011, 05:51 PM
Sorry posting the wrong SQL code, it's:


$db1 = new sql($dbhost, $dbuser, $dbpass, $dbname);
$dbs = $db1->rowSelect("SELECT * FROM tables WHERE id = {$_POST[id]}");
$db2 = new sql($dbs[5], $dbs[6], $dbs[7], $dbs[4]);
$chur = $db1->rowSelectRet("SELECT id, name FROM table");

Error:


Error in SQL: Table 'tables.table' doesn't exist - Query: SELECT id, name FROM table

The error happens in the second query string, the $chur, the first one works fine of course, then it opens the new object.

Fou-Lu
11-13-2011, 05:54 PM
You need to choose what you want to do. It doesn't make sense that static and non-static should ever be mixed. Forcing static on members guarantees that you cannot establish multiple connections since they will all share the same member properties, and even using if/else checks for is_object (which also doesn't make sense since this is always an object, regardless of if __construct has been called) doesn't change this.
Note as well that T::$t is not the same as $t->t. Lets see if we can write something to show this.


<?php

class T
{
static $t = 'default';
public static function getST()
{
return self::$t;
}
public function getT()
{
return $this->t;
}
}

$a = new T();
$b = new T();
$a->t = 'mkay';

T::$t = 'changed from default.';
printf('$a::$t=%s, $a->t=%s' . PHP_EOL, $a->getST(), $a->getT());
printf('$b::$t=%s, $b->t=%s' . PHP_EOL, $b->getST(), $b->getT());
?>

Run that. You'll see that ::$t <> ->t in both cases. Also in both cases you'll see that ::$t is equivalent between both $a and $b. I believe this is actually a bug in PHP, or at minimum an attempt to retain BC; you cannot redeclare a member property regardless of if one is static or dynamic, but you can create a dynamic member on the fly which corresponds to an identically named static property. Just add that to the stack of really confusing 'features' in PHP.


Oh, there's been about 4 more posts since I started writing this. Oh well.

Fou-Lu
11-13-2011, 06:05 PM
You are overcomplicating this class completely. Its simply an intermediary, you don't need to put this much work into it.
The only member you actually need is the connection resource, and POSSIBLY the last query if you want to keep it for whatever reason. You don't need to store anything else; the return doesn't appear to ever be used, the result is irrelevant since this isn't a Traversable type, and the numrows is also a little redundant. I also see globals in here which is just a bad idea. Either pass in or return variables that can be used instead, or configure anything necessary with additional member properties. I strongly recommend that you do not print from your methods. There is no rule that says you'll be using this in an HTML environment; return the results instead, or populate a provided variable with the results. Your goal should be to eliminate as many dependencies as you can between this class, other classes and configuration variables via global.

jfreak53
11-13-2011, 06:05 PM
You'll see that ::$t <> ->t in both cases. Also in both cases you'll see that ::$t is equivalent between both $a and $b. I believe this is actually a bug in PHP, or at minimum an attempt to retain BC; you cannot redeclare a member property regardless of if one is static or dynamic, but you can create a dynamic member on the fly which corresponds to an identically named static property. Just add that to the stack of really confusing 'features' in PHP.

So, does that mean I should create a NEW variable each time a new Object is called? Does $db get overwritten even though it's a new object and private?

That is what I'm getting at, is $db being overwritten?? Shouldn't it NOT be overwritten?

Sorry Fou, that confused me ha ha ha :thumbsup:

Fou-Lu
11-13-2011, 06:10 PM
$db is a dynamic property. Its never overwritten since it belongs to the memory scope provided by the new.


$a = new T();
$b = new T();
$a->t = 'a\'s t';
$b->t = 'b\'s t';

Neither can see each other unless you write a comparator so you can scope both. But then $this will never refer to the other, it will only refer to the calling class' $t.

Using a static $t will always result in a class level overwrite. That is what static is, it means that its scoped to the class, not an instance of that class. Useful in singletons.

jfreak53
11-13-2011, 06:13 PM
Ok, so then why if I am making the $db variable a private, why am I still getting the error?

Fou-Lu
11-13-2011, 06:20 PM
What error? This one? Error in SQL: Table 'tables.table' doesn't exist - Query: SELECT id, name FROM table ?
That's nothing to do with PHP, that's a SQL error. Since this is called with:


$db1 = new sql($dbhost, $dbuser, $dbpass, $dbname);
$dbs = $db1->rowSelect("SELECT * FROM tables WHERE id = {$_POST[id]}");
$db2 = new sql($dbs[5], $dbs[6], $dbs[7], $dbs[4]);
$chur = $db1->rowSelectRet("SELECT id, name FROM table");


The only answer's is that either the 'table' doesn't exist on a database referred to $db1 (possibly meant to be db2 if the database is different; $db1 and $db2 only share the same $db at an implicit level if the host, user, password are the same since you haven't requested that mysql_connect not re-use the connection), or that the SQL is simply invalid. I'm leaning towards the latter since 'table' is a reserved word in MySQL, and hasn't been wrapped in backticks to have it ignored by the MySQL parser as a command.

jfreak53
11-13-2011, 06:30 PM
Ok, I have changed the table names to temp ones for the post sake, those are not the real names. It's not an SQL error since if I make a new connection without OOP like so:


$db3 = @mysql_connect($dbs[5], $dbs[6], $dbs[7]) or die ('Cannot connect to database #1.');
@mysql_select_db($dbs[4], $db3);

$result = @mysql_query("SELECT id, name FROM table", $db3);
while($row = @mysql_fetch_array($result)) {
$ret1[] = array($row['ch_id'],$row['ch_name']);
}

mysql_close($db3);

That works just fine. Zero problems and it returns the results. The problem is PHP and the OOP because when I make the two separate connections as shown above is when it errors out. It is using the database from DB1 instead of DB2 as it should. That is the problem.

Fou-Lu
11-13-2011, 06:37 PM
I'm going to re-iterate this. This error: Error in SQL: Table 'tables.table' doesn't exist - Query: SELECT id, name FROM table is a SQL error. Not a PHP one. That is caused right here:


$return = mysql_query($qry, $this->db);
if(!$return) {
$this->error($qry, mysql_error(), $ret);
}


You need to debug the code to find out why. As I've also mentioned the call:


$db1 = new sql($dbhost, $dbuser, $dbpass, $dbname);
$dbs = $db1->rowSelect("SELECT * FROM tables WHERE id = {$_POST[id]}");
$db2 = new sql($dbs[5], $dbs[6], $dbs[7], $dbs[4]);
$chur = $db1->rowSelectRet("SELECT id, name FROM table");

Indicates that the error is occurring on $db1's connection, not on $db2. We don't know if $db1 and $db2 share the same connection since we don't know what these variables are. Back to the mysql_connect method:


If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.

The fourth parameter determines if the link can be shared. You do not specify this in your code, so although the class' $db properties are distinct, they both point at the same resource. Changing one will change both results, but not their properties.

jfreak53
11-13-2011, 06:46 PM
AHA!!! Thank you Fou. That is ALL I've been looking for this whole entire time! I did read the documentation first, but I had understood it to say that if the connection details were different then a new connection was created! :)

I didn't realize it changed it no matter what unless I put "true" in there. Now that was simple enough, and I had to go through all of this. That was it.

Thanks Fou for pointing out my block headedness.

Fou-Lu
11-13-2011, 06:53 PM
Okay, good. If you execute a var_dump on each of the object's themselves, you can see the private properties in them. If you use an external resource like a filepointer or SQL, check to see if the resource number provided is the same. If it is, they refer to the same pointer, otherwise, you have two distinct connections.
Now back to skyrim.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum