xProteuSx Posted May 17, 2016 Share Posted May 17, 2016 I've got something like this: $value = "SELECT rb_values.val_" . $_SESSION['add_value_year'] . ", rb_images.img_thumb, rb_images.img_lock, rb_notes.not_general, rb_notes.not_modern, rb_notes.not_specialized FROM rb_values, rb_images, rb_notes WHERE rb_values.val_id = $note[0] AND rb_images.img_id = $note[0] AND rb_notes.not_id = $note[0]"; Then I use mysql_fetch_assoc(), but I don't know how to retrieve the data for: rb_values.val_" . $_SESSION['add_value_year'] . " because that row could be val_2015, val_2014, val_2013, etc. depending on the value of $_SESSION['add_value_year']. I have tried using index number, but to no avail. I really don't know what to do here. Quote Link to comment https://forums.phpfreaks.com/topic/301196-mysql-query-using-variable-search/ Share on other sites More sharing options...
Solution xProteuSx Posted May 17, 2016 Author Solution Share Posted May 17, 2016 Got it working. Did this: $year_value = 'val_' . $_SESSION['add_value_year']; Then: $value = "SELECT rb_values." . $year_value . ", rb_images.img_thumb, rb_images.img_lock, rb_notes.not_general, rb_notes.not_modern, rb_notes.not_specialized FROM rb_values, rb_images, rb_notes WHERE rb_values.val_id = $note[0] AND rb_images.img_id = $note[0] AND rb_notes.not_id = $note[0]"; Then I could use: $row[$year_value] Quote Link to comment https://forums.phpfreaks.com/topic/301196-mysql-query-using-variable-search/#findComment-1533023 Share on other sites More sharing options...
xProteuSx Posted May 17, 2016 Author Share Posted May 17, 2016 Where the heck is the "Solved" button?? Quote Link to comment https://forums.phpfreaks.com/topic/301196-mysql-query-using-variable-search/#findComment-1533024 Share on other sites More sharing options...
mac_gyver Posted May 17, 2016 Share Posted May 17, 2016 a) you could use an alias name for the select term, then use use the alias name when you reference the data in php b) you should normalize your data. you should not have columns like that, where you must alter your database table any time you add data for a new year. the way you have your table laid out now, is not normalize, and results in more complicated queries to do anything with or find any of data. Quote Link to comment https://forums.phpfreaks.com/topic/301196-mysql-query-using-variable-search/#findComment-1533025 Share on other sites More sharing options...
cyberRobot Posted May 17, 2016 Share Posted May 17, 2016 Where the heck is the "Solved" button?? The topic has been marked solved. For future reference, the "Mark Solved" button is in the lower-right corner of each post within the topic. Quote Link to comment https://forums.phpfreaks.com/topic/301196-mysql-query-using-variable-search/#findComment-1533029 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.