I'm having a hard time understanding where is the problem...

I have a class which is responsible connecting to DB and passing queries.
(basically Data Access Layer)
and it looks like this:

class DB {
   private $connection;
   
   public function __construct(){
      $this->connection = mysql_connect('localhost', 'user', 'pass');
      mysql_select_db( 'dbname', $this->connection );
   }

   public function exec_query( $query ) {
      mysql_query($query, $this->connection );
   }
}

when I instantiate this class, everything is great... no errors
but when I call exec_query method, it prints the error:
supplied argument is not a valid MySQL-Link resource
and does not execute the query

Recommended Answers

Appears to be correct. Without your code it will be hard to tell. Do you check for errors (mysql_error()) ?

Jump to Post

Hi,
I agree with " pritaeas " your code is write.
I think only thing you will have to return query result to a function.
I tried it.
Your exec_query( $query ) should look like this.

public function exec_query( $query ) {
return mysql_query($query, $this->connection );
}

If you any …

Jump to Post

All 5 Replies

Appears to be correct. Without your code it will be hard to tell. Do you check for errors (mysql_error()) ?

Yes i did checked for errors and there were none!
It appears that the connection was made... and why shouldnt it connect?? its just on my local host.

My code is pretty much it, so there is not much more to add.

The thing is... if I make this class static, it worx just fine!!!

Hi,
I agree with " pritaeas " your code is write.
I think only thing you will have to return query result to a function.
I tried it.
Your exec_query( $query ) should look like this.

public function exec_query( $query ) {
return mysql_query($query, $this->connection );
}

If you any doubt please let me know.

Damn, wonder how I could've missed this ... +1

Golly I see loads of glitches in the code. Well their not exactly bugs since their not syntax errors but things that should be changed. Perhaps try the following:

class DB {
   private $connection;
 
   public function __construct(){
      $this->connection = mysql_connect('localhost', 'user', 'pass', true) or die(mysql_error());
      mysql_select_db( 'dbname', $this->connection ) or die(mysql_error());
   }
 
   public function exec_query( $query ) {
      $v=mysql_query($query, $this->connection ) or die(mysql_error());
      return $v;
   }
}

As you can see I added 'true' into the connect function, added a return statement into the exec_query() function and added debugging information in several places. Below is an example for using your class in case it was miss-used.

$con=new DB();
$result=$con->exec_query('SELECT * FROM `table`');
while ($row=mysql_fetch_assoc($result)) {
//loop through result here
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.