Jump to content

Need help with string search.


Beauford

Recommended Posts

I have a database that is going to have entries like the following:

 

_1_2_3_4

_3_4_5_6

_1_3_5_6

_2_4_5_6

 

 

After I select from the database I want to check to see if a number is in each row i.e. _3.

 

I am using the _ so that _3 and _33 are seperate entries.

 

I have tried every string function in PHP and just can't seem to get what I want.

 

 

Example of what I need:

 

$ur=$db->query("SELECT urVOTED FROM user_rating");

while($vote=$db->fetch_row($ur)) {

    if _3 is found in $vote['urVOTED'] { do something }

}

 

I know this is probably simple, but it's 2 in the morning and I've been at this way to long.

 

Thanks

Link to comment
Share on other sites

 

This, and a few variations of it, I tried and it does not find anything. If I put === False, it shows all of the entries don't contain the value, even though it does.

 

if (strpos('_3_', $vote['urVOTED'].'_') !== False)

 

This one seems to work. I tried similar to this, but not exactly as shown.

 

if (in_array('3', explode('_', $vote['urVOTED']))

 

Thanks for the effort.

 

 

Link to comment
Share on other sites

Why not just pull it out from the query?

 

$ur=$db->query("SELECT urVOTED FROM user_rating WHERE urVoted like '%_3_%'");
while($vote=$db->fetch_row($ur)) {
   // should now only contain the votes with _3_
}

 

If you want to do it in a loop for multiple cases, try this.

 

$ur=$db->query("SELECT urVOTED FROM user_rating");

while($vote=$db->fetch_row($ur)) {
     if (strstr($vote['urVOTED'], '_3_') !== false) {
          echo '_3_ was found!';
     }
}

Link to comment
Share on other sites

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.