MYSQL Select rows in PHP by name
I have the foll开发者_JAVA百科owing 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?
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.
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.
精彩评论