简体   繁体   English

从选择查询中获取强制转换值到数组中

[英]Fetch Casted Value from a select query into an array

I used cast in order to convert a datatype of one of the columns in my select query. 我使用cast来转换选择查询中列之一的数据类型。

SELECT cast(user_id as varchar(255)) from cabinet

I need to fetch the result of this value into an array but it returns null value 我需要将此值的结果提取到数组中,但它返回空值

$listData[] = array(
    "member_id" => $row['cast(user_id]
  );

How can I display it on array? 如何在阵列上显示?

SQL: SQL:

SELECT cast(user_id AS varchar(255)) AS member_id FROM cabinet

PHP: PHP:

$listData = array("member_id" => $row['member_id']);

Basically, you forgot to name the cast function's return in your SQL and used some extremely weird syntax in your PHP. 基本上,您忘记了在SQL中命名转换函数的返回值,而在PHP中使用了一些极其奇怪的语法。 $Array[] = $x; adds element $x as an array item to $Array in PHP. 将元素$x作为数组项添加到PHP中的$Array To create a new array, you should run $Array = array(); 要创建一个新数组,您应该运行$Array = array(); . Some programming languages do indeed force you to put square brackets after a variable's name on value assignation to specify it as an Array/String type, but PHP is not one of them. 确实有些编程语言确实强迫您在变量名称上的值分配后加上方括号以将其指定为Array / String类型,但是PHP并不是其中一种。

To reference an item by key, you have to use the array's name and the key's value in square brackets ( $Array[$n] , where $n is an integer for arrays with integer indexes, and Array['key'] for arrays with String indexes). 要按键引用项,您必须在方括号中使用数组的名称和键的值( $Array[$n] ,其中$n是具有整数索引的数组的整数,而Array['key']用于数组与字符串索引)。 'cast(user_id AS varchar(255))' is not a valid array key, you have to give the SQL function return value a name to be able to reference it as an array item. 'cast(user_id AS varchar(255))'不是有效的数组键,您必须给SQL函数返回值起一个名称,以便能够将其作为数组项引用。

Please let me know if this makes sense. 请让我知道这是否有意义。

My answer assumes that you are familiar with mySQLi and know how to get $row['member_id'] from the query. 我的答案假设您熟悉mySQLi并且知道如何从查询中获取$row['member_id']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM