954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MySql-Link resource

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

alex4700
Newbie Poster
3 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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!!!

alex4700
Newbie Poster
3 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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.

arunmagar
Light Poster
36 posts since Oct 2008
Reputation Points: 10
Solved Threads: 6
 

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

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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
}
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: