Check if field is equal to zero in the database

Asked

Viewed 302 times

-1

How do you make a query that checks in the database if a field is equal to zero (in PHP OOP)?

  • I advise you to put at least what you tried to do to get someone to help you.

  • 2

    Post the code you have already worked out or more details of your problem. A select * from table Where field = 0, solves.

2 answers

2

Try the following query:

SELECT campos FROM tabela WHERE campo = 0 -- Caso o campo seja numérico

SELECT campos FROM tabela WHERE campo LIKE '0' -- Caso o campo seja varchar

To reference in PHP, you will need to do all the "ritual":

<?php
// Cria a conexão
$host  = 'localhost'; // Endereço do servidor
$banco = 'meu_banco'; // Nome do banco de dados
$login = 'meu_login'; // Login no banco de dados
$senha = 'e_a_senha'; // Senha do banco de dados
$query = 'SELECT campos FROM tabela WHERE campo = 0';

try {
    // Conecta ao banco
    $pdo = new PDO('mysql:host='.$host.';dbname='.$banco, $login, $senha);

    // Prepara a consulta
    $consulta = $pdo->prepare($query);
    // Executa a consulta no banco
    $consulta->execute();
    // Faça o que for necessário com os dados
} catch (PDOException $ex) {
    exit('Erro ao carregar o banco de dados.');
}
?>

0

Try to do so, an IF.

$sql = "select * from tabela where campo = 'sua variavel';
$sth = $db->prepare($sql);
$sth->execute();
$record = $sth->fetch();
if ($sth->fetchColumn() > 0){
 //faz isso
}
else{
   faz isso...
}

Browser other questions tagged

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