Mick
Mick

Reputation: 2898

PHP foreach loop returning extra

I have a php script below that I am hoping would return all the values of a sql table. It does do this but also returns a number and value after each of the expected lines

    $condition = "usernames = '$user'  AND password = '$pass' ";
    $result = mysql_query("SELECT * FROM userbase WHERE $condition")  ;

    $row = mysql_fetch_array($result)  ;

    foreach ($row as $k => $v) {
        echo "$k / $v <br>";
        };

    ?>

The results look like this

      0 / Mick Lathrope 
      name / Mick Lathrope 
      1 / op 
      jobtitle / op 
      2 / 07998 783 989 
      phone / 07998 783 989 
      3 / mwl707 
      usernames / mwl707 
      4 / testpass
      password / testpass

All the data is there that i need But why am I also getting a number list ?

Any help please ?

Upvotes: 0

Views: 105

Answers (4)

miguel
miguel

Reputation: 1

According to this you have to specify the result type you want, otherwise you get both: MYSQL_ASSOC or MYSQL_NUM,

Example:

while ($row = mysql_fetch_array($result, MYSQL_NUM)){...}

Upvotes: 0

Heberfa
Heberfa

Reputation: 441

Use

$row = mysql_fetch_array($result, MYSQL_ASSOC) ;

to index only by column name

Upvotes: 2

moonwave99
moonwave99

Reputation: 22817

  1. drop mysql_* for PDO or mysqli;

  2. you want to use mysql_fetch_assoc to get just keys and no number indexes.

Upvotes: 0

Bhavik Shah
Bhavik Shah

Reputation: 2291

because you have used mysql_fetch_array.

If you want only columns then use mysql-fetch-assoc

Upvotes: 0

Related Questions