tags:

views:

313

answers:

5

Hi,

I'm trying to pass through an adodb connection to a class constructor but in doing so get this error:

mysql_error(): supplied argument is not a valid MySQL-Link resource

So to put it into context, I have instantiated a new adodb connection like this:

// establish database connection
$db = ADONewConnection($dsn);
if (!$db) die(mysql_error());

Then created my new user access object and passed in the adodb connection like this:

$user = new UserAccess($db);

This is the constructor from the user access class:

function UserAccess($oDbLink) {
   // check we have a valid connection
}

Any ideas what I'm doing wrong?

Thanks, Gaz

A: 

I can't see any obvious error in the part of code you supplied, so I'd suggest you:

  • leave aside the object for the time being
  • set your error_reporting() to E_ALL
  • double-check the parameters in $dsn - you may want to try to connect from the command line...
  • check your access privileges and run FLUSH PRIVILEGES
  • turn on ADODb debugging with $db->debug = TRUE;
  • test the thing from outside the object with a $db->Execute("SELECT * FROM tablename") or die($db->ErrorMsg());

When you get a message your connection resource is not a valid link - well, that's usually true. Don't forget to check the database is actually there and running.

djn
Djn - thanks for your suggestions - I had tried them beforehand.
Gaz
A: 

The problem is most likely that you're attempting to use PHP's mysql_* functions with the $db object you get from ADONewConnection. It's not a mysql handle, so it won't work with those functions - you need to use adodb's own stuff.

Jani Hartikainen
Hi - Jani Hartikainen,I'm only using the mysql_* to grab the error if the adodb class fails, its not used anywhere else.I can the error when I try and pass the database handler to the user class.Would you suggest bringing the adodb stuff inside the user class?
Gaz
You should add the line of code which is causing the error into your question.
Jani Hartikainen
Jani - sorry if I wasn't clear. This is the actual line that's causing the error:$user = new UserAccess($db);Thanks again
Gaz
It can't be that because that line does nothing with the link. If you're saying it's caused by the constructof for UserAccess, you should include the code in it or otherwise it's gonna be difficult to say.
Jani Hartikainen
Jani - I have posted the constructor code above - that's all I have at the moment. I've decided to create a global function that connects to the database then pass a reference to that function in my user class
Gaz
A: 

Nothing wrong with your code, from what you've supplied. If you supply something more full then may be able to help.

Is there a real reason for passing a ADODB connection around? It'll just need to be passed everywhere and more work and without a real reason... I'd hesitate to do that. Even a standard global may be better (yes globals are evil) or perhaps a Singleton class?

Forbes Myester
A: 

Your code should be like this:

// establish database connection
$db = ADONewConnection($dsn);
if ( ! $db )
{
  // just display error message
  // you cannot use mysql_error, since you haven't connected to any database
  die ("Cannot connect, check the parameter and make sure db is running!");
}

User reference, to avoid copying object

function UserAccess(&$oDbLink) {
 // check we have a valid connection
}

Hope it help.

Donny Kurnia
A: 

Thanks for your answers, I've managed to get this working by removing the $dsn variable and just passing in $_GLOBAL variables

Gaz