Read phpMyAdmin column

Asked

Viewed 85 times

2

I was able to separate some values from a column in a table in Phpmyadmin through some tips and I did this:

mysql_select_db($database_conexao, $conexao);
if ($dep != ""  and $sub != "") {
    $query_rsPesquisa = "SELECT * FROM  `produtos` WHERE id_departamento = $dep AND id_subdepartamento = $sub";
} else {
    $query_rsPesquisa = "SELECT * FROM  `produtos` WHERE id_departamento = $dep";
}
$rsPesquisa = mysql_query($query_rsPesquisa, $conexao) or die(mysql_error());
$row_rsPesquisa = mysql_fetch_assoc($rsPesquisa);
$totalRows_rsPesquisa = mysql_num_rows($rsPesquisa);

$string = $row_rsPesquisa['id_subfiltro'];  
$array = explode(',', $string); 

And then, to get a description of each id in a specific table and show on my page, I did this:

<?php

if ($dep != "" and $sub != "") {
    foreach ($array as $valores) {

        mysql_select_db($database_conexao, $conexao);
        $query_rsSub = "SELECT * FROM subfiltro WHERE id_subfiltro = $valores";
        $rsSub = mysql_query($query_rsSub, $conexao) or die(mysql_error());
        $row_rsSub       = mysql_fetch_assoc($rsSub);
        $totalRows_rsSub = mysql_num_rows($rsSub);

        $descProd = $row_rsSub['descricao'];

        echo "<ul class='menu2'>
                    <li><a href='#'>$descProd</a></li>
                    <li class='current-menu-item'></li>                
                  </ul>";            
    }
}
?>

But what’s weird is that my first select not getting all the values from my column id_subfilter, it seems that in the first, see the image below:

Tabela

This is the page: Developing page

The values that in this case need to rescue would 8,2,61,155,6,17,16,176,117.

But it’s returning to me:

Array ( [0] => 8 [1] => 2 [2] => 61 [3] => 155 [4] => 6 [5] => 17 )
  • Running this query in phpMyAdmin, the value of this column is also cut?

  • Hello @Lucas, no, the result appears full.

  • In my view, have you solved right? Post here the resolution.

  • Hello Lollipop, I managed to resolve yes, but I still have some doubts that need to be properly addressed.

  • Just a hint: mysql_* functions are obsolete since PHP 5.5. Prefer to use Mysqli or PDO. See more here: http://www.ultimatephp.com.br/php-por-que-nao-utilizar-funcoes-mysql

1 answer

1


The big problem I was having was that I was not able to store the records after using the explode, as I had been doing a lot of testing on this page I ended up forgetting to make a loop to scan the options and store them correctly. The solution I found was simple, look at this:

    // defini a variável como array
    $subGrupos = array();

    do {

        $string = $row_rsPesquisa['id_subfiltro'];
        // separei os registros por vírgula     
        $array = explode(',', $string); 
        // uni a variável $subGrupo com o array
        $subGrupos = array_merge($subGrupos, $array);   

    } while ($row_rsPesquisa = mysql_fetch_assoc($rsPesquisa));

    // desconsiderei os valores repetidos.
    $array = array_unique($subGrupos);

Here is the code, I thank everyone who helped me in the comments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.