1

My question is fairly simple. I have a column in my table labeled 'area' that appears like:

ma_south-coast
ma_south-caost
ma_north
ca_los-angelos
ca_los-angelos

I want to be able to select just the 'ma' ones. I am trying to do something such as:

$res_area = mysqli_query($connection,"select area from users");
        while ($row_state = mysqli_fetch_array($res_area)) {
        $state_exp = reset(explode("_", $row_state['area']));
        }

Printing $state_exp at this point would give me: mamamacaca which is good. But I want to filter so I only get the ma ones.

3
  • You need to use filter - explode will not work for you as you would hope. php.net/manual/en/function.array-filter.php It would be preferable if you did this in your SQL query using LIKE or SUBSTRING. Commented Aug 20, 2014 at 16:26
  • Are you looking to do this with your SQL query or in PHP? Commented Aug 20, 2014 at 16:26
  • Thank you @JayBlanchard I wasn't sure if it would. I could maybe do it in the SQL query? I am imagining something like select area from users where [the current value] == [first two letters of a variable using explode] Commented Aug 20, 2014 at 16:37

4 Answers 4

2

You can extend your query with WHERE column LIKE "ma%" or check with substr($row_state['area],0,2) if the first two characters are "ma".

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

Comments

1

You could try this:

$res_area = mysqli_query($connection,"select area from users");
        while ($row_state = mysqli_fetch_array($res_area)) {
        $state_exp = stristr(reset(explode("_", $row_state['area'])),'ma');
        }

1 Comment

My scenario was best to filter using "like" in my sql query. However this is an interesting approach as well. I'll end up using this function sooner or later I'm sure.
1

You're looking for the LIKE operator. The LIKE operator is used to search for a specified pattern in a column. Try this:

        SELECT * FROM tablename WHERE areaLIKE 'ma_%';

You can read more about the LIKE operator here.

1 Comment

perfect! glad to hear & happy to help! :0)
-1

The optimal solution could be :-

$res_area = mysqli_query($connection,"select area from users WHERE area LIKE 'ma_*')";
        while ($row_state = mysqli_fetch_array($res_area)) {
        if(stripos(reset(explode("_", $row_state['area'])),'ma') !== FALSE)
        {
           $state_exp = reset(explode("_", $row_state['area']));
        }
      }

3 Comments

I like how this is a definitive approach, as opposed to relying on PHP magic to figure out what is "like" what. Idk... the magic is working for me currently.
@Jbreen37 Glad i could help u!. But why the Downvote ?.wud be gr8 if it can be turn into upvote.
@Jbreen37 oh Sorry. Neways my badluck!

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.