将 adodb 连接传递给新的 php 对象

发布于 2024-08-14 18:21:13 字数 547 浏览 5 评论 0原文

我试图将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

因为看清所以看轻 2024-08-21 18:21:13

我在您提供的代码部分中看不到任何明显的错误,因此我建议您:

  • 暂时保留该对象,将
  • 您的 error_reporting() 设置为 E_ALL
  • 仔细检查 $dsn 中的参数 - 您可能想尝试从命令行连接...
  • 检查您的访问权限并运行 FLUSH PRIVILEGES
  • 使用 $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:

  • 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.

白首有我共你 2024-08-21 18:21:13

问题很可能是您尝试将 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 from ADONewConnection. It's not a mysql handle, so it won't work with those functions - you need to use adodb's own stuff.

是伱的 2024-08-21 18:21:13

从您提供的代码来看,您的代码没有任何问题。如果您提供更全面的东西,那么可能会有所帮助。

传递 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?

花伊自在美 2024-08-21 18:21:13

您的代码应该是这样的:

// 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!");
}

用户引用,以避免复制对象

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

希望它有帮助。

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.

帝王念 2024-08-21 18:21:13

感谢您的回答,我已经通过删除 $dsn 变量并仅传递 $_GLOBAL 变量来设法使其工作

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文