在对我的mssql数据库执行select语句时删除前导的零列

nwo49xxi  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(252)

嘿,伙计们,我有微软sql管理工作室18,在那里我有一个数据库。我通过php做了一个select语句,如下所示:

$conn = OpenCon();

            $query = "SELECT id, name, picture, description, numberOfEngines FROM planes";
            $result = sqlsrv_query($conn, $query);

            if ($result === false) {
                $status['status'] = "0";
                echo json_encode($status);
            }
            else{
                while($row = sqlsrv_fetch_array($result)) {
                    $theRows[] = $row;
                }
                echo json_encode($theRows);
            }
            CloseCon($conn);

这是输出:

[{"0":1,"id":1,"1":"114","name":"114","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C525","description":"Cessna C525","4":1,"numberOfEngines":1},
{"0":2,"id":2,"1":"115","name":"115","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C525","description":"Cessna C525","4":1,"numberOfEngines":1},
{"0":3,"id":3,"1":"124","name":"124","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C208B","description":"Cessna C208B","4":1,"numberOfEngines":1},
{"0":4,"id":4,"1":"125","name":"125","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C208B","description":"Cessna C208B","4":1,"numberOfEngines":1}]

如何删除这些重复出现两次的前导字符,如“0”:1或ariplane.png。所以我的输出是这样的:

[{"id":1, "name":"114", "picture":"airplane1.png", "description":"Cessna C525", "numberOfEngines":1}]
2wnc66cl

2wnc66cl1#

若要仅返回数组中的关联键,请传递 SQLSRV_FETCH_ASSOC 作为 fetchType 参数到 sqlsrv_fetch_array :

while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {

这将为您提供一个包含如下项的数组:

{"id":1,"name":"114","picture":"airplane1.png","description":"Cessna C525","numberOfEngines":1}

如果您真的需要其他数字键,请保持代码不变并添加

unset($row[0])

之前

$theRows[] = $row;

相关问题