0

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 ?

1
  • Besides moving to MySQLi or PDO, look at the second argument for MySQL_fetch_array() and in particular the default value Commented Feb 16, 2013 at 15:20

4 Answers 4

2

Use

$row = mysql_fetch_array($result, MYSQL_ASSOC) ;

to index only by column name

Sign up to request clarification or add additional context in comments.

Comments

0

because you have used mysql_fetch_array.

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

Comments

0
  1. drop mysql_* for PDO or mysqli;

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

Comments

0

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)){...}

Comments

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.