Count positions of an SQL field

2

How do I manage to count each position in this array? I was working with SQL queries what happens is that I can not get the positions .

    
asked by Marcial Cahuaya Tarqui 22.12.2016 в 22:37
source

2 answers

0

You can consult it normally and then use the native Php function called explode

$query = "SELECT 'resultados' FROM tabla";

After you get the query, you apply explode:

$result = $conn->query($query);

$result = $result->fetch_array(MYSQLI_ASSOC);

// Obtengo un array con cada elemento separado por paid ("|")
    $resultados = explode('|', $result);

$cantidad_resultados = count($resultados);

See the explode documentation for better compression.

    
answered by 22.12.2016 / 23:19
source
0

If what you need is to add from your SQL, you can try

SELECT  SUM((LEN(campo] ) - LEN(REPLACE(campo, 'RAISERROR 50000', '')))/LEN('RAISERROR 50000')) as suma FROM    table

But if you need to do it from your PHP

You can use:

  

str_word_count ()

Example:

$cadena = "Esto es una prueba para contar palabras";
echo "$cadena";

// I take the number of words with the parameter '0'

echo "Hay ".str_word_count($cadena, 0). " palabras en la cadena '$cadena'";

// I keep the words in an array

$array_cadena = str_word_count($cadena, 1);

// I take out each element of the array

foreach ($array_cadena as $palabra) {
echo $palabra . " ";
}
    
answered by 22.12.2016 в 22:44