Jump to content

MySQL Query Using Variable Search


Go to solution Solved by xProteuSx,

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/301196-mysql-query-using-variable-search/
Share on other sites

  • Solution

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]

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.