Phone book with database

1

I have a phone book with the form already ready and now I need that when the user type in the field to search for a name or sector, this value must be compared with values of the database and a value corresponding to the search of it is returned.

Follow my PHP code so far;

$bdServidor = '';
$bdUsuario = '';
$bdsenha = '';
$bdBanco = 'lista_telefonica';

$conexao = mysqli_connect($bdServidor, $bdUsuario, $bdsenha,$bdBanco);

if(mysqli_connect_errno($conexao))
{
    echo"problemas para conectar no banco.Verifique os dados!";
    die();
}

// Variavel que contem o valor do campo BUSCA;
$valor = $_POST['busca'];
    
asked by anonymous 18.11.2014 / 15:09

2 answers

3

To do this you will need to do a search ( SELECT ) in the Database and use the LIKE to check for the word in that field.

$sql = "SELECT id, name FROM lista_telefonica WHERE nome LIKE '%$valor%' OR setor LIKE '%$valor%"; 

if ($result = query($conexao, $sql)) 
{
    while($obj = $result->fetch_object())
    {
        $line =$obj->id;
        $line.=$obj->name;
    }
}
$result->close();
unset($obj);
unset($sql);
unset($query); 

PS: Do not forget to use Prepared Statements because of the SQL INJECTION

    
18.11.2014 / 15:27
1

You should use a query SELECT something like this:

<?php
 $consulta=$ pdo->query("SELECT nome, usuario FROM login;");

 while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) 
{ echo "Nome: {$linha['nome']}
 - Usuário: {$linha['usuario']}
<br />"; } ?>

Look for more about CRUD, it's something simple to do with your SELECT . As for comparing you should use in%% of a checker, such as% of SQL%.

Here's an example of CRUD

Ah remembering that on your form you have to get the variable's get.

Follow the steps in this tutorial above and find out about SELECT .

    
18.11.2014 / 15:26