1

I have the following code and it only works when I specify the column number!

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    echo($row[0]);
}

Not when I use the name though

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    echo($row['name']);
}

Is there something that needs setting on the MYSQL box?

1
  • Are you sure you have a name column in your resultset ? (do you have such a column in your table, and is it selected) Commented Mar 12, 2011 at 14:51

2 Answers 2

4

You might want to try print_r( $row ) for debugging so you see which column names are actually set in the resultset. Getting the associative names doesn't need to be configured in some way, but the index names need to exactly represent the column names in the database result.

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

1 Comment

Yep +1 and accept, I was being a num nuts as the row was called tablename_columnname
0

mysql_fetch_array, according to the documentation, fetches the result row as an numeric array or as an associative array, depending on the second parameter. By default, this is MYSQL_BOTH, so you can access via both means.

It seems like the problem may like with your query, specifically the actual name of the column in your SELECT statement. Without seeing more of your query, I can suggest adding AS name to the first column to ensure it is the actual retrieved column name. print_r will print the array for further debugging.

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.