我正在考虑更改一些 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.dll
和 php_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?
发布评论
评论(5)
不是一个直接的答案,但也许它可以帮助您搜索它:
遗留的 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.
看来您所需要做的就是取消注释
php.ini
中正确的extension
指令,例如extension=php_mysqli.dll
和extension =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 inphp.ini
, likeextension=php_mysqli.dll
andextension=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"
inphp.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 inc:\php
instead of having it buried several levels deep inc:\xampp
.)If anybody wants to follow along with installing your server components separately, I documented the whole process, with screenshots, on my blog.
与 为什么这个 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
我找到的独特解决方案:
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
好吧,我有一个非常糟糕但可行的解决方案: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 ;)