jasonc Posted August 2, 2010 Share Posted August 2, 2010 i am wanting to use the following code but it is not working as i thought it would. instead of hard coding the field name in the script i wish to have it insert using a variable like below. what is the correct way i should do this. $c= "fieldname"; $a = mysql_result( $results, 0, $c); Quote Link to comment https://forums.phpfreaks.com/topic/209574-a-mysql_result-b-0-c-does-not-work/ Share on other sites More sharing options...
Adam Posted August 2, 2010 Share Posted August 2, 2010 Your code is syntactically correct provided that $results is a valid MySQL result resource. Without seeing more of the code we can't really say why it's not working. Quote Link to comment https://forums.phpfreaks.com/topic/209574-a-mysql_result-b-0-c-does-not-work/#findComment-1094073 Share on other sites More sharing options...
jasonc Posted August 2, 2010 Author Share Posted August 2, 2010 how do i print the result on the screen so i can paste it here i am using the following to get the data $sql = "SELECT * FROM `users` WHERE `user` = 'me' LIMIT 1"; Quote Link to comment https://forums.phpfreaks.com/topic/209574-a-mysql_result-b-0-c-does-not-work/#findComment-1094089 Share on other sites More sharing options...
Yesideez Posted August 2, 2010 Share Posted August 2, 2010 As you're only getting one row back you can use something like this: $query=mysql_query($sql); $row=mysql_fetch_assoc($query); echo $row['name']; That's presuming the field "name" appears in your table. Quote Link to comment https://forums.phpfreaks.com/topic/209574-a-mysql_result-b-0-c-does-not-work/#findComment-1094093 Share on other sites More sharing options...
jasonc Posted August 2, 2010 Author Share Posted August 2, 2010 ok but can i use something like this? $fieldname = "name"; $query=mysql_query($sql); $row=mysql_fetch_assoc($query); echo $row[$fieldname]; Quote Link to comment https://forums.phpfreaks.com/topic/209574-a-mysql_result-b-0-c-does-not-work/#findComment-1094097 Share on other sites More sharing options...
Yesideez Posted August 2, 2010 Share Posted August 2, 2010 The first thing to do is give it a go but yes - you can do that. Quote Link to comment https://forums.phpfreaks.com/topic/209574-a-mysql_result-b-0-c-does-not-work/#findComment-1094101 Share on other sites More sharing options...
jasonc Posted August 2, 2010 Author Share Posted August 2, 2010 but i tried that as in my first post, and it did not work $c= "fieldname"; $a = mysql_result( $results, 0, $c); Warning: mysql_result(): supplied argument is not a valid MySQL result resource in line $a = mysql_result( $results, 0, $c); $c = "username"; and there is a field in the mysql database called "username" Quote Link to comment https://forums.phpfreaks.com/topic/209574-a-mysql_result-b-0-c-does-not-work/#findComment-1094103 Share on other sites More sharing options...
Yesideez Posted August 2, 2010 Share Posted August 2, 2010 $sql="SELECT * FROM users WHERE user='me' LIMIT 1"; $query=mysql_query($sql); $row=mysql_fetch_assoc($query); echo $row[$col]; Quote Link to comment https://forums.phpfreaks.com/topic/209574-a-mysql_result-b-0-c-does-not-work/#findComment-1094109 Share on other sites More sharing options...
Adam Posted August 2, 2010 Share Posted August 2, 2010 Warning: mysql_result(): supplied argument is not a valid MySQL result resource in line Would have been helpful to quote this error earlier. $results 'is not a valid MySQL result resource'. Try amending to the end of your mysql_query() call: or trigger_error('MySQL error: ' . mysql_error()); Make sure you don't have a semi-colon before 'or'.. i.e. "; or trigger(...)" Quote Link to comment https://forums.phpfreaks.com/topic/209574-a-mysql_result-b-0-c-does-not-work/#findComment-1094188 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.