4

Ok i execute this

$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!;
    }

and i do foreach

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

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

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
    )

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 ..

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

2
  • 3
    Use mysql_fetch_array($result, MYSQL_ASSOC). By default, it returns both numeric and column names as indices. I'll tend to recommend mysql_fetch_assoc() to skip the issue anyway. Commented Mar 4, 2012 at 16:51
  • 1
    possible duplicate of mysql_fetch_array and only string array keys Commented Mar 4, 2012 at 16:52

1 Answer 1

17

From the manual:

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

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

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I am new to php ( started 1-2 days ago). And again thank you for your fast answer
changing from mysql_fetch_array to mysql_fetch_row solved my doubling up for a while ($row=mysql_fetch_row($query))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.