2017-10-04 24 views
-1

我有一个MySQL表,其中我跑左联接的查询:MySQL返回阵的钥匙和Tabelle-名称 - 删除键

$db = $db->first("SELECT * FROM devices LEFT JOIN customer ON devices.customerid = customer.customerid WHERE devices.hash = '".$hash."' OR devices.deviceid = '".$hash."'"); 

使用此功能:

//Select Datenbankanfrage, erste Zeile 
function first($query, $restype=0) { 
    $this->querystring = $query; 

    if ($restype==1) $restype=MYSQLI_ASSOC; 
    else $restype=MYSQLI_BOTH; 

    $result = $this->query($query); //Query 
    if (!$result) return false; 

    $row = $result->fetch_array($restype); 
    $result->free(); 

    return $row; 
} 

但我收到一个array与键作为回应:

1 => Maxii 
Name => Maxii 
2 => Streetfromthe 
Street => Streetfromthe 

怎么可以我禁用了键(1,2)或者我该如何在数组中删除它?

这里是从全阵列Nopaste返回: https://nopaste.xyz/?23ccf318bbfc93fb#Oes85/9Z8+Kookk2nTLmbtObjaL8mD2aitPPrHV3v9w=

+0

它看起来像链接到你的粘贴是不完整的。 –

+0

你传递给函数'first'的原因是第二个参数'$ restype'决定你是否应该获取关联数组或者是关联数组和数字索引数组。对于这个论点,你可能看起来像是通过了别的不是1的东西。 – OptimusCrime

+0

谢谢,这是工作!哈哈。 Resttype = 1 – Burhan

回答

0

问题来自$ restype,您的第二个参数。它默认为零,它将使用MYSQLI_BOTH,它通过索引和名称返回字段。你一定要一个关联数组(MYSQLI_ASSOC),传递$ restype为1

,负责改变restype的线路有:

if ($restype==1) $restype=MYSQLI_ASSOC; 
    else $restype=MYSQLI_BOTH; 
+0

谢谢你的解释! – Burhan

0

您需要$result->fetch_assoc

编辑以取代$result->fetch_array:在你的情况下,留下它在fetch_array并使用您的功能。 使用first($sql, 1);调用它

+0

我尝试过,然后我成为一个错误。 – Burhan

+0

@Burhan如果你发布了错误消息,它会有帮助。 – OptimusCrime

+0

这是因为这个别名,有第二个参数。将它保存在'fetch_array'处,但是将一个额外的参数传递给第一个函数,1);所以它只会只有ASSOC。正如你的代码所述。更新回答 –