我的数组(mysql_fetch_array)中的结果翻倍 [英] double results in my array ( mysql_fetch_array )

查看:65
本文介绍了我的数组(mysql_fetch_array)中的结果翻倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我执行这个

$table = get_personel_table(1);
function get_personel_table($id)
    {
        global $connection;
        $query  = "SELECT * ";
        $query .= "FROM employees ";
        $query .= "WHERE id=" . $id . " ";
        $query .= "ORDER BY id ASC";
        $query_result = mysql_query( $query , $connection );
        confirm_query($query_result);
        $query_result_array = mysql_fetch_array($query_result);
        return $query_result_array; // returns associative array!;
    }

我做foreach

foreach($table as $table_var)
{
    echo "<td>" . $table_var . "</td>";
} 

这样做,我得到了双重输出..."1 1 1 1 jordan jordan 9108121544 9108121544 testEmail testEmail testAddress testAddress testCounty testCounty"

and by doing so i get double output ... "1 1 1 1 jordan jordan 9108121544 9108121544 testEmail testEmail testAddress testAddress testCounty testCounty"

这是print_r的结果

this below is the result of print_r

 Array
    (
        [0] => 1
        [id] => 1
        [1] => 1
        [department_id] => 1
        [2] => jordan
        [name] => jordan
        [3] => 9108121544
        [EGN] => 9108121544
        [4] => testEmail
        [email] => testEmail
        [5] => testAddress
        [address] => testAddress
        [6] => testCounty
        [country] => testCounty
    )

我在数据库中拥有的信息是id => 1,department_id => 1,依此类推... 我的问题是为什么我会得到双重反馈(我不知道该怎么称呼),0 = id,1 = department_id,2 =名称等等.

The information i have in the database is id =>1 , department_id => 1 , and so on ... My question is why i get double feedback(i don't know how to call it) , 0 = id , 1 = department_id , 2 = name and so on ..

当我做foreach(...)时,我得到的一切都会翻倍.

and when i do foreach( ... ) i get everything doubled.

推荐答案

手册:

mysql_fetch_array —提取结果行为关联数组,数字数组或两者兼有

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

默认情况下,mysql_fetch_array给出关联索引和数字索引.你不要这个您可以使用第二个参数来限制它:

By default, mysql_fetch_array gives both associative and numeric indexes. You don't want this. You can limit it with the second parameter:

$query_result_array = mysql_fetch_array($query_result, MYSQL_NUM); // numeric keys only
$query_result_array = mysql_fetch_array($query_result, MYSQL_ASSOC); // associative keys only

您还可以使用 mysql_fetch_row 仅获取数字键,或使用

You can also use mysql_fetch_row to only get numeric keys, or mysql_fetch_assoc to only get associative keys.

$query_result_array = mysql_fetch_row($query_result); // numeric keys only
$query_result_array = mysql_fetch_assoc($query_result); // associative keys only

这篇关于我的数组(mysql_fetch_array)中的结果翻倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆