Php View Columns

Asked

Viewed 325 times

0

I have two columns A and B with mixed words, I want to remove, the whole row of A and B that in column B has less than 8 characters.

how do I do this in php?

  • example: testing|12345678 - but I want to view only those that contain 8 characters above |

  • Do you want to mount a Query that only displays lines where B has more than 8 characters? Or do you want to delete lines that have less than 8?

  • Beware of the question as removing the line can make sense of Delete

5 answers

1

You can use the functions strlen and explode to help you.

The strlen function returns the size of a string Already function explodes create an array dividing the string (2nd parameter) based on the delimiter (1st parameter ).

Example:

$string = " testando|12345678 ";

if( strlen(explode("|", $string)[1]) < 8 )
    remover_linha();

1

I think it is more efficient and simple if executed by mysql

$query = "SELECT id, A, B FROM table WHERE CHAR_LENGTH(B) >=8";

$recordset = mysqli_query($query, $link);

0

Use the strlen.

if (strlen($palavra) > 8) {
//aqui exibi
}

0

//Busca os dados na tabela
$query = "SELECT id, colunaA, colunaB FROM tabela";
//Executa a query
$result = mysqli_query($query, $link);
//Cria uma variávelaleatória para armazenar os IDs das linhas a serem removidas
$array[] = '';
//Rotina de repetição para percorrer todos os resultados
while($row = mysqli_fetch_array($result)){
    //Verifica se a coluna B tem menos de 8 caracteres
    if(strlen($row['colunaB']) < 8){
        //Se a coluna B tiver menos de 8 caracteres, adiciona o ID da linha em questão na array
        $array[] = $row['id'];
    }
}
//Agora a array contém apenas os IDs das linhas que precisam ser removidas
foreach ($array as $key) {
    //Cria a query com um ID de cada vez
    $query = "DELETE FROM tabela WHERE id = $key";
    //Executa a query removendo uma linha de cada vez
    mysqli_query($query);
}

I think so. I tried to do it in an easy way to understand.

0

To list only lines containing column B with more than 8 characters:

<?php 
    $pdo = Connection::Conexao(); // Conexão com o banco
    $sql = "SELECT * FROM TABELA";
    $query = $pdo->prepare($sql);           
    $query->execute();

    while ($dados = $query->fetch(PDO::FETCH_ASSOC)) {

        if(strlen($dados["coluna_B"]) > 8) {
            // mostrando coluna A e B somente se B tiver mais de 8 caracteres
            echo $dados["coluna_A"]." - ".$dados["coluna_B"]."<br />";
        } else {

            // você também pode inserir aqui um código para deletar os registros onde
            // b for menor que 8
        }
    }
?>

Browser other questions tagged

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