3

Hi! I want to search string having value like "CMS" in array $row, below code is searching for exact "CMS".

if (mysql_num_rows($sql_result)>0)
  { 
    while ($row = mysql_fetch_assoc($sql_result))
        { $counter++; 
           if (in_array("CMS", $row)) 
               { $cms++; }
          }
    }

I'll be grateful if any one can guide me to how to search string for value LIKE "CMS" in array $row.

2
  • It will more usefull to use SQL to count your data. Commented Apr 4, 2014 at 9:46
  • You'd better do the job with sql but not in php side. Commented Apr 4, 2014 at 9:47

3 Answers 3

3

Use strpos and serialize

if (mysql_num_rows($sql_result)>0)
  { 
    while ($row = mysql_fetch_assoc($sql_result))
        { $counter++; 
           if (strpos(serialize($row),"CMS")!==false)  //<--- Changed this `if` statement, nothing else
               { $cms++; }
          }
    }

You could serialize your array and then do a strpos to check whether the needle is inside the array or not. This is something similar like doing a LIKE as you requested.

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

Comments

0

Loop over each of the columns in the row and use strpos

if (mysql_num_rows($sql_result)>0) 
{ 
    while ($row = mysql_fetch_assoc($sql_result))
    { 
        $counter++; 
        foreach($row as $column)
        {
            if (strpos($column, 'CMS'))
            {
                $cms++;
            }
        }
    }
}

Comments

0
if (mysql_num_rows($sql_result)>0)
  { 
    while ($row = mysql_fetch_assoc($sql_result))
        { $counter++; 
           foreach ($row as $column) {
              if (strpos($column, "CMS") !== false) {
                  $cms++;
              }
           } 

        }
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.