We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,451 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

4
Contributors
5
Replies
13 Hours
Discussion Span
2 Years Ago
Last Updated
6
Views
alex4700
Newbie Poster
3 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,313 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,465
Skill Endorsements: 86

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
Skill Endorsements: 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
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,313 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,465
Skill Endorsements: 86

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: 260
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.2885 seconds using 2.8MB