将 adodb 连接传递给新的 php 对象
我试图将 adodb 连接传递到类构造函数,但这样做会出现此错误:
mysql_error(): supplied argument is not a valid MySQL-Link resource
因此为了将其放入上下文中,我实例化了一个新的 adodb 连接,如下所示:
// establish database connection
$db = ADONewConnection($dsn);
if (!$db) die(mysql_error());
然后创建了新的用户访问对象并传入adodb 连接如下:
$user = new UserAccess($db);
这是用户访问类的构造函数:
function UserAccess($oDbLink) {
// check we have a valid connection
}
有什么想法我做错了吗?
谢谢, 天然气
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在您提供的代码部分中看不到任何明显的错误,因此我建议您:
error_reporting()
设置为E_ALL
$dsn
中的参数 - 您可能想尝试从命令行连接...$db 打开 ADODb 调试->debug = TRUE;
$db->Execute("SELECT * FROM tablename") 或 die($db->ErrorMsg()); 从对象外部测试事物。
当您收到一条消息时,您的连接资源不是有效链接 - 嗯,通常是这样。不要忘记检查数据库是否确实存在并且正在运行。
I can't see any obvious error in the part of code you supplied, so I'd suggest you:
error_reporting()
toE_ALL
$dsn
- you may want to try to connect from the command line...$db->debug = TRUE;
$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.
问题很可能是您尝试将 PHP 的 mysql_* 函数与从
ADONewConnection
获取的$db
对象一起使用。它不是 mysql 句柄,所以它不能与这些函数一起工作 - 你需要使用 adodb 自己的东西。The problem is most likely that you're attempting to use PHP's mysql_* functions with the
$db
object you get fromADONewConnection
. It's not a mysql handle, so it won't work with those functions - you need to use adodb's own stuff.从您提供的代码来看,您的代码没有任何问题。如果您提供更全面的东西,那么可能会有所帮助。
传递 ADODB 连接是否有真实原因?它只需要到处传递和更多的工作,并且没有真正的原因......我会犹豫是否要这样做。即使是标准的全局变量也可能更好(是的,全局变量是邪恶的),或者也许是 Singleton 类?
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?
您的代码应该是这样的:
用户引用,以避免复制对象
希望它有帮助。
Your code should be like this:
User reference, to avoid copying object
Hope it help.
感谢您的回答,我已经通过删除 $dsn 变量并仅传递 $_GLOBAL 变量来设法使其工作
Thanks for your answers, I've managed to get this working by removing the $dsn variable and just passing in $_GLOBAL variables