Apache 需要什么才能同时支持 mysqli 和 PDO?

发布于 2024-08-09 03:38:12 字数 1908 浏览 7 评论 0 原文

我正在考虑更改一些 PHP 代码以使用 PDO 进行数据库访问,而不是 mysqli (因为 PDO 语法对我来说更有意义并且与数据库无关)。为此,我需要在进行转换时同时使用这两种方法。

我的问题是这样的:到目前为止,其中一种方法或另一种方法都会使 Apache 崩溃

现在我在 Windows XP 中使用 XAMPP,以及 PHP 版本 5.2.8。 Mysqli 工作正常,这也是如此:

$dbc = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
echo 'Connected to database';
$sql = "SELECT * FROM `employee`";

但是这一行使 Apache 崩溃:

$dbc->query($sql);

我不想重做整个 Apache 或 XAMPP 安装,但我希望 PDO 能够工作。所以我尝试从 libmysql.dll “nofollow noreferrer”>此处,正如 oddvibes 推荐的此处。这使得我的简单 PDO 查询能够工作,但随后 mysqli 查询导致 Apache 崩溃。

(我还尝试了在那之后的建议,更新 php_pdo_mysql.dllphp_pdo.dll,但没有效果。)

测试用例

我创建了这个测试脚本来比较 PDO 与mysqli。对于 libmysql.dll 的旧副本,如果 $use_pdo 为 true,则崩溃;如果为 false,则不会崩溃。对于 libmysql.dll 的新副本,情况恰恰相反。

if ($use_pdo){
  $dbc = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
  echo 'Connected to database<br />';
  $sql = "SELECT * FROM `employee`";
  $dbc->query($sql);
  foreach ($dbc->query($sql) as $row){
    echo $row['firstname'] . ' ' . $row['lastname'] . "<br>\n";
  }

}
else {
  $dbc = @mysqli_connect($hostname, $username, $password, $dbname) OR die('Could not connect to MySQL: ' . mysqli_connect_error());
  $sql = "SELECT * FROM `employee`";
  $result = @mysqli_query($dbc, $sql) or die(mysqli_error($dbc));

  while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
    echo $row['firstname'] . ' ' . $row['lastname'] . "<br>\n";
    }
}

Apache 需要什么才能支持这两种数据库查询方法?

I'm considering changing some PHP code to use PDO for database access instead of mysqli (because the PDO syntax makes more sense to me and is database-agnostic). To do that, I'd need both methods to work while I'm making the changeover.

My problem is this: so far, either one or the other method will crash Apache.

Right now I'm using XAMPP in Windows XP, and PHP Version 5.2.8. Mysqli works fine, and so does this:

$dbc = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
echo 'Connected to database';
$sql = "SELECT * FROM `employee`";

But this line makes Apache crash:

$dbc->query($sql);

I don't want to redo my entire Apache or XAMPP installation, but I'd like for PDO to work. So I tried updating libmysql.dll from here, as oddvibes recommended here. That made my simple PDO query work, but then mysqli queries crashed Apache.

(I also tried the suggestion after that one, to update php_pdo_mysql.dll and php_pdo.dll, to no effect.)

Test Case

I created this test script to compare PDO vs mysqli. With the old copy of libmysql.dll, it crashes if $use_pdo is true and doesn't if it's false. With the new copy of libmysql.dll, it's the opposite.

if ($use_pdo){
  $dbc = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
  echo 'Connected to database<br />';
  $sql = "SELECT * FROM `employee`";
  $dbc->query($sql);
  foreach ($dbc->query($sql) as $row){
    echo $row['firstname'] . ' ' . $row['lastname'] . "<br>\n";
  }

}
else {
  $dbc = @mysqli_connect($hostname, $username, $password, $dbname) OR die('Could not connect to MySQL: ' . mysqli_connect_error());
  $sql = "SELECT * FROM `employee`";
  $result = @mysqli_query($dbc, $sql) or die(mysqli_error($dbc));

  while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
    echo $row['firstname'] . ' ' . $row['lastname'] . "<br>\n";
    }
}

What does Apache need in order to support both methods of database query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

紫瑟鸿黎 2024-08-16 03:38:12

不是一个直接的答案,但也许它可以帮助您搜索它:

遗留的 mysql 扩展(以 mysql_ 为前缀的函数)是 libmysqlclient 的一个薄包装器。新的 mysqli 扩展本质上是相同的,但它实现了 libmysqlclient 更高版本中引入的一些功能。 PDO 也使用 libmysqlclient,但不像其他扩展那样直接映射它。这总共相当于 3 个不同的 php 扩展,它们都引用同一个本机库。如果其中一些人对库的版本做出假设,则可能会导致他们发生冲突。

我建议您安装可以找到的最新版本的 libmysqlclient.dll 并尝试禁用旧版 mysql 扩展(如果您还没有这样做)。

如果您有使用 mysql 扩展的代码,则可以将 mysqli 绑定到这些函数,并且它应该以相同的方式工作。

另外,请确保您没有因某种原因安装新的 mysqlnd 驱动程序。 mysqlnd 是 libmysqlclient 的替代实现,它实际上使一切变得更加复杂。

是的,这是一个大混乱。

Not a direct answer, but perhaps it can help you in your search for it:

The legacy mysql extension (The function prefixed with mysql_) is a thin wrapper over libmysqlclient. The new mysqli extension is essentially the same, but it implements some functionality that were introduced in later versions of libmysqlclient. PDO also uses libmysqlclient, but doesn't map it as directly as the other extensions do. This all amounts to 3 different php-extensions that all refer to the same native library. If some of them make assumptions about the version of the library, it might cause them to clash.

I would suggest that you install the newest version of libmysqlclient.dll that you can find and try to disable the legacy mysql extension (if you haven't already).

If you have code that uses mysql extension, you can have mysqli bind to those functions and it should work the same.

Also, make sure that you don't have the new mysqlnd driver installed for some reason. mysqlnd is an alternative implementation of libmysqlclient and it really just makes everything even more complicated.

Yes, it's a big mess.

゛清羽墨安 2024-08-16 03:38:12

看来您所需要做的就是取消注释 php.ini 中正确的 extension 指令,例如 extension=php_mysqli.dllextension =php_pdo_mysql.dll

我从未用 XAMPP 确认过这一点,因为我对此感到沮丧,并决定单独安装 Apache、PHP 和 MySQL。在这个过程中,我学会了执行上述步骤,以及许多其他事情。所以我还没有确认这是否可以与 XAMPP 一起使用,但我不明白为什么不可以,除非它没有配备正确的驱动程序。

上面示例指令中的两个驱动程序(.dll 文件)是我正在使用的驱动程序。我还在 php.ini 中设置了 extension_dir = "c:/php/ext" - 您应该确保该路径指向您的 php 扩展所在的目录。 (请注意,单独安装这些组件的好处之一是我可以将 PHP 放入 c:\php 中,而不是将其深埋在 c:\xampp 中几层>.)

如果有人想单独安装服务器组件,我在我的博客上记录了整个过程,并附有屏幕截图

It seems like all you need is to uncomment the correct extension directives in php.ini, like extension=php_mysqli.dll and extension=php_pdo_mysql.dll.

I never confirmed this with XAMPP, because I got frustrated with it and decided to install Apache, PHP and MySQL individually. In the process, I learned to do the above steps, along with a lot of other things. So I haven't confirmed that this will work with XAMPP, but I don't see why not, unless it doesn't come with the right drivers.

The two drivers (.dll files) in the example directives above are the ones I'm using. I also set extension_dir = "c:/php/ext" in php.ini - you should make sure that path points to the directory where your php extensions are. (Note that one of the nice things about installing these components separately is that I can put PHP in c:\php instead of having it buried several levels deep in c:\xampp.)

If anybody wants to follow along with installing your server components separately, I documented the whole process, with screenshots, on my blog.

雪若未夕 2024-08-16 03:38:12

为什么这个 pdo::mysql 代码在 Windows 上崩溃?

但暂时没有解决方案

崩溃仅发生在“SELECT * FROM xxx”query() 和准备/执行上

same as Why does this pdo::mysql code crash on windows?

but dont have the solution for the moment

Crash happens only on "SELECT * FROM xxx" query() and prepare/execute

习惯那些不曾习惯的习惯 2024-08-16 03:38:12

我找到的独特解决方案:

Install LibMySQL.DLL FROM 5.0.51a package (与最新的 5.1.44 MySQL Version 一起使用)

http://www.netfulvpc.fr/files/libmysql_dll.zip

The unique solution i found :

Install LibMySQL.DLL FROM 5.0.51a package (working with last 5.1.44 MySQL Version)

http://www.netfulvpc.fr/files/libmysql_dll.zip

在梵高的星空下 2024-08-16 03:38:12

好吧,我有一个非常糟糕但可行的解决方案:p)

用十六进制编辑器打开 ext\php_pdo_mysql.dll

搜索“83 C3 50”偏移 0x000024d5(在 php 5.2.12 vc6 中)

替换为 83 C3“54”

问题是结构大小错误...(struct pdo_column_data)

我尝试在 libmysql.dll 中修复该问题,但导致 php_mysqli 崩溃;)

Ok, i have a very bad but working solution :p)

Open ext\php_pdo_mysql.dll with an Hex Editor

Search for "83 C3 50" offset 0x000024d5 (in php 5.2.12 vc6)

Replace by 83 C3 "54"

The problem is a bad structure size... (struct pdo_column_data)

I tried to fix that in libmysql.dll but that crash php_mysqli ;)

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