2012-12-17 151 views
-3

我的代码是:奇怪mysql_fetch_array输出

$query = "SELECT a.hash, a.full, a.partial, a.servers, a.lastupd, b.full, b.partial, b.servers, b.lastupd FROM table1 AS a, table2 AS b WHERE a.hash='".$hashcode."' AND a.hash=b.hash;"; 
$result = mysql_query($query); 

然后我执行

print_r(mysql_fetch_array($result)); 

而且我得到这个奇怪的输出:

Array 
(
    [0] => blah 
    [hash] => blah 
    [1] => 1,1,1 
    [full] => 0,1 
    [2] => 1,2,1 
    [partial] => 4,2 
    [3] => srv1,srv2,srv3 
    [servers] => srv2,srv3 
    [4] => 0000-00-00 00:00:00 
    [lastupd] => 0000-00-00 00:00:00 
    [5] => 0,1 
    [6] => 4,2 
    [7] => srv2,srv3 
    [8] => 0000-00-00 00:00:00 
) 

为什么有数据重复的b表不会创建一个关联数组,其关键字为a.*b.*

注:设置为0的时间戳是正确

回答

5

因为mysql_fetch_array同时返回关联和数字数组。使用mysql_fetch_assoc,如果你只需要关联数组。

(顺便说一下,MySQL是过时,read about using mysqli or PDO

+0

'mysql_fetch_assoc'返回一个关联数组,但只是用'a.hash'和从'B'表中的数据。我需要改进我的查询吗?我怎样才能从'a'表中获取数据呢? – Vektor88

+1

是的,你必须有不同的别名,如果在表中你有列FULL,那么你需要重新命名表B列为FULLB(只是一个例子)。 b.full AS'fullb'等 – Moseleyi

+0

这解决了这个问题 – Vektor88