通过各自的值访问PHP MySQLI预定义常量? [英] Accessing PHP MySQLI predefined constants via respective value?

查看:123
本文介绍了通过各自的值访问PHP MySQLI预定义常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个函数,该函数采用mysqli_result并将所有返回的列添加到我的结果对象中的关联数组中。到目前为止,通过mysqli :: multi_query返回的所有内容均为字符串格式,而有关应保留哪种值类型的描述保存在mysqli :: fetch_fields返回的对象数组中。我觉得到目前为止,这还是很令人困惑的,所以让我把它写出来

I am writing a function that takes a mysqli_result and adds all of the returned columns into an associative array in my result object. As of right now everything that is returned via the mysqli::multi_query is in string format, while the description as to what value type the value should be is kept in the array of objects returned by mysqli::fetch_fields. I feel that so far this is pretty confusing so let me code it out

if ($mysqli->multi_query($inputQuery)) {
    if ($result = $mysqli->store_result()) {
        //$fields is an array of custom objects
        $fields = $result->fetch_fields();

        //$fieldType is an integer that is the value of a mysqli predefined constant
        $fieldType = $fields[0]->type;

        if ($curRow = $result->fetch_row()) {
            //No matter what the value type in the database of this item is, and no
            //matter what $fieldType corresponds to, $curVal is going to be a string
            $curVal = $curRow[0];
        }
    }
}

<$ c的整数据我了解,$ c> $ fieldType 的值是预定义的mysqli常数的值,可以在以下位置找到: http://www.php.net/manual/en/mysqli.constants.php

The integer that $fieldType takes is, from my understanding, the VALUE of a predefined mysqli constant which can be found here: http://www.php.net/manual/en/mysqli.constants.php

我知道与此类似的问题已发布在这里: PHP mysqli_fetch_field数据类型但我对将值映射回它们各自的键更感兴趣。

I know a similar question to this has been posted here: PHP mysqli_fetch_field data type but I am more interested in mapping the values back to their respective keys.

我想做的是将字符串值转换为根据 $ fieldType 到mysqli预定义常量的映射,将$ curVal 转换为正确的值类型。我想我可以对每个预定义的常量进行强制检查,或者我可以使用value-> key而不是key-> value创建一个关联数组,然后引用它,但是我觉得应该有一种更简单的方法来解决这个问题

What I would like to do is cast the string value of $curVal into the correct value type based on the mapping of $fieldType to the mysqli predefined constant. I suppose that I could brute force check against every predefined constant, or I could create an associative array with value -> key instead of key -> value and then reference that, but I feel like there should be an easier way to go about this.

那么,我的问题是,通过PHP的值查找预定义常量的最简单方法是什么?我是否要像在PHP中访问全局常量一样访问这些mysqli预定义常量,还是必须执行 $ mysqli-> PREDEFINED_CONSTANT

My question, then, is what is the easiest way to find a predefined constant in PHP by its value? Do I access these mysqli predefined constants the same way that I access global constants in PHP or do I have to do something like $mysqli->PREDEFINED_CONSTANT?

最诚挚的问候,

推荐答案

常量不是对象的一部分,因此使用$ mysqli: :CONSTSANT或$ mysqli-> CONSTANT无法那样工作。

The constants are not part of the object so using $mysqli::CONSTSANT or $mysqli->CONSTANT won't work that way.

这是示例的开始。您必须查看 MySQLi常数的其余部分并完成 switch 语句。

Here's the beginning of an example. You'll have to look at the rest of the MySQLi constants and finish the switch statement. The ones that aren't included will remain strings.

$result = $mysqli->query('SELECT * FROM `accounts`');
$fields = $result->fetch_fields();

// Loop through each row.
while ( $row = $result->fetch_row() )
{
  // Loop through each field.
  foreach ( $row as $key => &$value )
  {
    // Using the $key, find the type of the current field.
    switch ( $fields[$key]->type )
    {
      // Convert INT to an integer.
      case MYSQLI_TYPE_TINY:
      case MYSQLI_TYPE_SHORT:
      case MYSQLI_TYPE_LONG:
      case MYSQLI_TYPE_LONGLONG:
      case MYSQLI_TYPE_INT24:
        $value = intval($value);

        break;
      // Convert FLOAT to a float.
      case MYSQLI_TYPE_FLOAT:
      case MYSQLI_TYPE_DOUBLE:
        $value = floatval($value);

        break;
      // Convert TIMESTAMP to a DateTime object.
      case MYSQLI_TYPE_TIMESTAMP:
      case MYSQLI_TYPE_DATE:
      case MYSQLI_TYPE_DATETIME:
        $value = new DateTime($value);

        break;
    }
  }

  var_dump($row);
}

这篇关于通过各自的值访问PHP MySQLI预定义常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆