0

I have table 'nar' with one of columns with name 'meno', its VARCHAR type. There is numbers of rows, about 5000. I need count how many times for example letter "K" is in all rows in column 'meno'.

I tried something, but it doesnt working:

    $query5 = "SELECT length(`meno`)-length(replace(`meno`,'K','')) AS letter FROM nar";
    $result5 = mysql_query($query5);
    $row5 = mysql_fetch_array($result5);
    $pocetPismen = $row5['letter'];
    echo '<br>Pocet pismena K: '.$pocetPismen.'<br>';

Any advice? Thanks

Stanley01
  • 55
  • 8
  • https://stackoverflow.com/questions/13467901/mysql-count-letters#13468660 – Matt Jun 20 '17 at 20:58
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 20 '17 at 20:59
  • Here si solution, maybe helps somebody: `$query5 = 'SELECT SUM(CHAR_LENGTH(meno)-CHAR_LENGTH(REPLACE(MENO,"K",""))) AS pocet FROM nar WHERE date > "2017-01-01"'; $result5 = mysql_query($query5); $row5 = mysql_fetch_array($result5); $pocetPismen = $row5['pocet']; echo '
    Pocet pismena K: '.$pocetPismen.'
    ';`
    – Stanley01 Jun 20 '17 at 21:26

0 Answers0