0

I am not that good at PHP and i am stuck right now.

I have this code in one of my table rows in my db. Danish_English_Swedish. The data is from the table teacher.

I have this Query: $db->prepare("SELECT * FROM teacher WHERE subject = ? ORDER BY id DESC");

I have created an AJAX request which is called when a <select> is changed. This request is sent to a page that contains the above query. This select contains options as danish, english and swedish.

example:

If i select Danish in the <select> and a teacher has a row containing Danish_English_Swedish i want to grab the Danish from the data so it mathes the users request.

Maybe you can implode/explode and make it to an array and check it that way?

Any solutions are welcome!

3
  • 2
    If you're encoding multiple values into a single column that's probably a violation of the Zero, One or Infinity Rule of database normalization. Underscore-separated is really an irregular format as well. Commented Aug 10, 2017 at 22:40
  • 1
    Those should be separate columns in the database. Commented Aug 10, 2017 at 22:43
  • 1
    I agree you should normalize your database. Always. Do it now. As for your question, you could use FIND_IN_SET('Danish',REPLACE('_',',','Danish_English_Swedish')) in the WHERE part of your select. Commented Aug 10, 2017 at 22:45

1 Answer 1

1

You need LIKE statement

$db->prepare("SELECT * FROM teacher WHERE column LIKE ? ORDER BY id DESC");

NOTE 1: change column name to the name of column where Danish_English_Swedish is contained

NOTE 2: check collation of your database/table if it is _ci (case-insensitive), otherwise you'd like to do strtolower before running query and also store values in lower case in table

NOTE 3: from the description you provided I have feeling, that your database structure and table columns are not well-optimized for working with them at all, but for your particular case answer is above.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! All i needed
Plus one for note 3!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.