简体   繁体   English

PHP Oracle OCI8 查询以保留/处理数组中的空列

[英]PHP Oracle OCI8 query to keep/handle null columns in array

I have the below sql query.. in which I'm mapping the contents to a kendo front-end table..我有以下 sql 查询 .. 在其中我将内容映射到剑道前端表..

$sql = "SELECT big_id, fac_ident, lg_name, basic_tp, catc_vd, address, assoc_city, latitude, longitude, assoc_cnty, assoc_st, time_zone, faa_reg, ato_sa, ato_td, fema_reg, ops_hrs, prim_ph, atc_level, tower_type, manager, sat_phone_num, td_mgr, to_dist, tod_mgr, stof_fac, ops_status, crt_rl FROM myCoolApp.big_id";

The problem is.. columns that are not finding any associated value in the DB, is not returning any value back, and alas just completely omitted from the response array.. ie in ops_hrs above no value is found, so it's completely omitted from my array - I need it to still exist, just be blank.问题是.. 在数据库中没有找到任何关联值的列,没有返回任何值,唉,只是从响应数组中完全省略了.. 即在上面的ops_hrs没有找到任何值,所以它从我的数组 - 我需要它仍然存在,只是空白。

I would like the response to still display the column in the array even when it's value is empty ;我希望响应仍然显示数组中的列,即使它的值为 ideally just adding a blank space " " or 'N/A' even.理想情况下,只需添加一个空格" "'N/A' As I do not want the null column to be dropped in my array and front-end.因为我不希望在我的数组和前端中删除空列。

The rest of my PHP/sql...我的 PHP/sql 的其余部分...

        $stid = oci_parse($this->db, $sql);
        oci_execute($stid); 
            
        $myData = array();

        while ($list = oci_fetch_array($stid, OCI_ASSOC)) {
            array_push($myData, $list);
        }
    
        header('Content-Type: application/json');
        echo "{\"data\":" .json_encode($myData). "}";

What am I missing here?我在这里缺少什么? I need to handle it programmatically with PHP/sql.我需要用 PHP/sql 以编程方式处理它。

Use OCI_RETURN_NULLS :使用OCI_RETURN_NULLS

while ($list = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
    array_push($myData, $list);
}

See the PHP OCI8 oci_fetch_array documentation .请参阅PHP OCI8 oci_fetch_array文档

As of SQL (and Oracle), you could use the NVL function which returns column value if it exists, or its "substitute" if it does not.对于 SQL(和 Oracle),您可以使用NVL函数,如果存在则返回列值,如果不存在则返回“替代”。

For example, in this query comm column's value is NULL :例如,在此查询comm列的值为NULL

SQL> select ename, comm from emp where empno = 7369;

ENAME            COMM
---------- ----------
SMITH

You'd then那时你会

SQL> select nvl(ename, 'N/A') as ename,
  2         nvl(comm , 0    ) as comm
  3  from emp
  4  where empno = 7369;

ENAME            COMM
---------- ----------
SMITH               0

SQL>

Pay attention to column's datatype.注意列的数据类型。 If it is a string, NVL should return a string as well (such as 'N/A' );如果是字符串,则NVL应该返回一个字符串(例如'N/A' ); if it is a number, let it return a number (such as 0 ), etc.如果它是一个数字,让它返回一个数字(例如0 )等。

There's no "universal" way to do it, so you'll have to apply NVL to all columns you're selecting.没有“通用”的方法可以做到这一点,因此您必须将NVL应用于您选择的所有列。

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

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