0

I have a table called country_zones and theres a column called country which currently holds

["canada","United States"]

I first, extract the users country they signed up with and then need to do a query to see which users country matches the row of country_zones. Although whenever I do that I either get error or NULL

Ive tried this.

  $country = json_decode($this->db->get_where('country_zones',array('country'=>$user_country)));

also.

$country = $this->db->query("SELECT * FROM country_zones WHERE country='$user_country'")->result();
4
  • And what do you get from your attempts? Commented Feb 9, 2017 at 18:06
  • Show more of the code as what you show is not of much help to us Commented Feb 9, 2017 at 18:07
  • what is dataType for country column? and share $user_country value? Commented Feb 9, 2017 at 18:08
  • Datatype is LongText, and I just have a query to extract users country aka, It displays 'Canada' thats what '$user_country' holds. Now I need to find the row in the country_zones table which holds 'canada' and return it Commented Feb 9, 2017 at 18:11

1 Answer 1

1

Let's say user_country is Canada, then your query should be:

SELECT * FROM country_zones WHERE country LIKE '%"Canada"%';

So, try this:

$country = $this->db->query("SELECT * FROM country_zones WHERE country LIKE '%\"$user_country\"%'")->result();

Edit:

LIKE query is needed because column contains ["canada","United States"] and you want to match it with Canada. So above LIKE query is SQL way of saying give me rows where country contains "Canada".

See: https://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html

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

3 Comments

It works! Can you just explain to me why I need to use LIKE and what the '%\ is for??
Thank you. And now if I want to extract just the country row can I do $thecountry = $country->row()->country; ??
What do you mean by "extract just the country row"? Also, PHP is not my forte, I can help with MySQL though.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.