dongme8388 2017-06-21 03:31
浏览 35
已采纳

从选择查询中将Casted Value提取到数组中

I used cast in order to convert a datatype of one of the columns in my select query.

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?

  • 写回答

1条回答 默认 最新

  • douwen5066 2017-06-21 03:42
    关注

    SQL:

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

    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. $Array[] = $x; adds element $x as an array item to $Array in PHP. To create a new array, you should run $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.

    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). '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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)