1,105,578 Community Members

MySql-Link resource

Member Avatar
alex4700
Newbie Poster
3 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

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

Member Avatar
alex4700
Newbie Poster
3 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
arunmagar
Junior Poster in Training
51 posts since Oct 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
1
 

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.

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

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

Member Avatar
cwarn23
Occupation: Genius
3,028 posts since Sep 2007
Reputation Points: 387 [?]
Q&As Helped to Solve: 281 [?]
Skill Endorsements: 15 [?]
Team Colleague
Featured
 
0
 

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
}
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: