【发布时间】:2020-09-28 03:17:37
【问题描述】:
好的,我执行这个
$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”
下面这个是 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 等等..
当我做 foreach( ... ) 时,我得到的一切都翻了一番。
【问题讨论】:
-
使用
mysql_fetch_array($result, MYSQL_ASSOC)。默认情况下,它返回数字和列名作为索引。我倾向于推荐mysql_fetch_assoc()来跳过这个问题。