从 mysql 转变为 mysqli

发布于 2024-09-15 19:00:35 字数 574 浏览 6 评论 0原文

我对 PHP 中的 mysqli 接口有点困惑。当我昨天开始将旧的 mysql 代码更改为 mysqli 时,文档引导我走上 ::prepare::bind_params::execute 的道路

在我努力处理结果的过程中,我最终编写了自己的 fetch_assoc - 并且遇到了我在 SO 上讨论的问题。一位评论员质疑我在不使用 ::fetch_assoc 的情况下做了什么。我什至没有注意到 mysqli_result 类。当我将 mysqli 代码从 ::preapre 更改为 ::query 时,结果处理变得更加容易 - 但有一个例外:之前我使用 ::bind_results 时 提取列,生成的 PHP 关联数组将携带正确的数据类型;当我现在使用 ::fetch_assoc 等时,我得到的只是一个字符串数组。

我现在正在编写一个转换器。我只是想,既然我在理解 mysqli 库时已经犯了一个重大错误,我最好在这里发帖看看是否有一种“官方”方法来获取类型一致的关联结果数组。

I am a little perplexed by the mysqli interface in PHP. When I set out yesterday to change my old mysql code to mysqli, the docs steered me down the road of ::prepare, ::bind_params and ::execute.

In my efforts to deal with the results, I ended up writing my own fetch_assoc - and had problems which I discussed on SO. One commentator queried what I was doing not using ::fetch_assoc. I had not even noticed the mysqli_result class. When I morphed my mysqli code from ::preapre to ::query, the result handling became MUCH easier - with one exception: when previously I used ::bind_results to extract the columns, the PHP associative array that resulted would carry the correct data types; when instead I now use ::fetch_assoc etc, all I get is an array of strings.

I am coding up a converter now. I just thought that since I already committed one cardinal error in my understanding of the mysqli library, I better post here to see whether there's an "official" way of getting type-congruent associative result arrays.

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

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

发布评论

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

评论(1

独木成林 2024-09-22 19:00:35

如果您使用 mysqlnd 作为传输客户端并设置选项 MYSQLI_OPT_INT_AND_FLOAT_NATIVE在 mysqli 对象上,您实际上获得了本机类型。

echo 'phpversion: ', phpversion(), "\n";

$m = new mysqli('localhost', 'localonly', 'localonly', 'test');
$m->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);
$m->query('CREATE TABLE soTest (x int)');
$m->query('INSERT INTO soTest (x) VALUES (1),(2)');

$r = $m->query('SELECT x from soTest');
var_dump($r->fetch_assoc());

prints

phpversion: 5.3.3
array(1) {
  ["x"]=>
  int(1)
}

这个功能是在 PHP 5.3 中添加的。


为了让您开始使用 PDO (如果您选择使用它):

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// the pdo_mysql driver uses emulated prepared statements by default
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

// afaik only the mysqlnd client supports native types, with libmysql you'll get only strings/null
echo 'client: ', $pdo->getAttribute(PDO::ATTR_CLIENT_VERSION), "\n";

// set up test environment
$pdo->exec('CREATE TEMPORARY TABLE soTest (x int, y varchar(16))');
$pdo->exec("INSERT INTO soTest (x,y) VALUES (1,'a'),(2,null)");

// statement with positional parameter
$stmt = $pdo->prepare('SELECT x,y FROM soTest WHERE x>?');
$stmt->setFetchMode(PDO::FETCH_ASSOC);

$stmt->execute( array(0) );
foreach( $stmt as $row ) {
  foreach( $row as $col ) {
    echo gettype($col), '(', $col, ') ';
  }
  echo "\n";
}

prints

client: mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
integer(1) string(a) 
integer(2) NULL() 

If you're using mysqlnd as the transport client and set the option MYSQLI_OPT_INT_AND_FLOAT_NATIVE on the mysqli object you get in fact the native types.

echo 'phpversion: ', phpversion(), "\n";

$m = new mysqli('localhost', 'localonly', 'localonly', 'test');
$m->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);
$m->query('CREATE TABLE soTest (x int)');
$m->query('INSERT INTO soTest (x) VALUES (1),(2)');

$r = $m->query('SELECT x from soTest');
var_dump($r->fetch_assoc());

prints

phpversion: 5.3.3
array(1) {
  ["x"]=>
  int(1)
}

This feature has been added in PHP 5.3.


To get you started with PDO (should you choose to use it):

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// the pdo_mysql driver uses emulated prepared statements by default
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

// afaik only the mysqlnd client supports native types, with libmysql you'll get only strings/null
echo 'client: ', $pdo->getAttribute(PDO::ATTR_CLIENT_VERSION), "\n";

// set up test environment
$pdo->exec('CREATE TEMPORARY TABLE soTest (x int, y varchar(16))');
$pdo->exec("INSERT INTO soTest (x,y) VALUES (1,'a'),(2,null)");

// statement with positional parameter
$stmt = $pdo->prepare('SELECT x,y FROM soTest WHERE x>?');
$stmt->setFetchMode(PDO::FETCH_ASSOC);

$stmt->execute( array(0) );
foreach( $stmt as $row ) {
  foreach( $row as $col ) {
    echo gettype($col), '(', $col, ') ';
  }
  echo "\n";
}

prints

client: mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
integer(1) string(a) 
integer(2) NULL() 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文