OriginalSixRules Posted April 26, 2010 Share Posted April 26, 2010 I am executing the following query. Please note the first column name: $sqlString="SELECT href, urlParam, urlParamVal, linkText, target FROM menulinks WHERE menu='leftSideMenu' "; $result = mysql_query($sqlString); The following code: while ($thisRow = mysql_fetch_row($result )){ echo $thisRow['href']; } results in this error: Notice: Undefined index: href in C:\EasyPHP-5.3.2\www\my_app\display\leftSideMenu.php on line 25 If I go to output the column names with this code: echo "The column names are-"; $numRows=mysql_num_rows($result ); $i = 0; while ($i < mysql_num_fields($result )){ $field = mysql_fetch_field($result , $i); echo $field->name."<br/>"; $i++; } The output from the above code is... The column names are- href urlParam urlParamVal linkText target I am obviously selecting the column named 'href,' and the column name can be displayed as selected. If I change the code in the first loop to use $thisRow[0] I get the output I expect. In fact, if I use any of the column names instead of numbers I get the "invalid index" error. What simple mistake am I making and/or missing? Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted April 26, 2010 Share Posted April 26, 2010 use mysql_fetch_assoc instead. mysql_fetch_row returns a numerically indexed array. mysql_fetch_assoc returns an associative array. Quote Link to comment Share on other sites More sharing options...
OriginalSixRules Posted April 26, 2010 Author Share Posted April 26, 2010 Yeesh. To quote the Zen master Homer Simpson as he claps with one hand - "D'oh!!!!" Thanks for the help! Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 26, 2010 Share Posted April 26, 2010 Personally, I always use mysql_fetch_assoc(), but as an alternative there is also mysql_fetch_array() which can retrieve the results as an enumerated array, an associative array or both (default). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.