我的数组中的双重结果( mysql_fetch_array ) [英] double results in my array ( 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 = name 等等..
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
来只获取数字键,或者mysql_fetch_assoc
只获取关联键.
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屋!