Bring data from Table and returns no results

Asked

Viewed 50 times

0

Guys I’m having a problem searching data in a table, follow code.

PHP CODE

<?php
$servidor = "localhost";
$usuario = "vitor";
$senha = "";
$banco = "funcionarios_db";
$mysqllink = mysqli_connect("$servidor","$usuario","$senha","$banco");

<?php

$parametro = filter_input(INPUT_GET, "parametro");
if($parametro){
    $dados = mysqli_query("SELECT * FROM funcionario WHERE funcionario LIKE '%$parametro%' ORDER BY id_matricula");
}
    else {
        $dados = mysqli_query($mysqllink, "SELECT * FROM funcionario ORDER BY id_matricula");

}

$linha = mysqli_fetch_assoc($dados);
$total = mysqli_num_rows($dados); ?>

TABLE

<table border="1">
<tr>
    <td>ID Matrícula</td>
    <td>Funcionário</td>
    <td>Cargo</td>
    <td>Alocado</td>
</tr>
<?php
    if($total){ do{

?>
<tr>
    <td><?php echo $linha['ID Matrícula'] ?></td>
    <td><?php echo $linha['Funcionário'] ?></td>
    <td><?php echo $linha['Cargo'] ?></td>
    <td><?php echo $linha['Alocado'] ?></td>
</tr>
<?php 
    } while($linha = mysqli_fetch_assoc($dados));
    mysqli_free_result($dados);}

    mysqli_close($mysqllink);
?>

People are returning this from my table:

inserir a descrição da imagem aqui

  • Variable does not escape with single quotes only with double quotes, I think the mistake is this. Try replacing "SELECT * FROM funcio WHERE funcio LIKE '%$parametro%' ORDER BY id_matricula" by 'SELECT * FROM funcionario WHERE funcionario LIKE %". $parameter." % ORDER BY id_matricula'

  • Doing some kind of query in the database with GET parameter?

  • Vinicius didn’t work out

  • @Viniciusdejesus I believe that is not the problem, since the single quotes refer to the like parameter and the variable $parameter is inside double quotes.

  • @Joãolima is having some mistake? if yes could inform which? Hugs.

  • @Gustavo Luciano unfortunately no, he only creates the work with the table columns, and does not display the bank lines

  • How so just create table? table is no longer created?

  • @Vinicius de Jesus I did an EDIT in the post and put the table image.

  • normally use a for as a repetition structure to print data from a table, mainly because of the variable that makes the iteration be used as index of the array.

  • look at my answer down there and return to me.

Show 5 more comments

3 answers

0


Replace your code with this.

<?php
$servidor = "localhost";
$usuario = "vitor";
$senha = "";
$banco = "funcionarios_db";
$mysqllink = mysqli_connect("$servidor","$usuario","$senha","$banco");


$parametro = "";
if(isset($_GET['parametro'])) {
    $parametro = $_GET['parametro'];
}
//


$sql = ("SELECT * FROM funcionario WHERE funcionario LIKE '%$parametro%' ORDER BY id_matricula");
$result = mysqli_query($mysqllink, $sql) or die(mysqli_error());
$linha = mysqli_fetch_assoc($result);
$total = mysqli_num_rows($result);

if($total > 0 ) { ?>


<table border="1">
<tr>
    <td>ID Matrícula</td>
    <td>Funcionário</td>
    <td>Cargo</td>
    <td>Alocado</td>
</tr>

<?php do { ?>
<tr>
    <td><?php echo $linha['ID Matrícula'] ?></td>
    <td><?php echo $linha['Funcionário'] ?></td>
    <td><?php echo $linha['Cargo'] ?></td>
    <td><?php echo $linha['Alocado'] ?></td>
</tr>
<?php } while($linha = mysqli_fetch_assoc($result)); ?>

</table>        


<?php   }

    //
    else {

        echo 'Nenhuma consulta para esta matricula.';

    }
    //
?>
  • didn’t work yet colleague, didn’t bring any database results, only brought the count of data lines that my database has, in case 4.

  • tries to replace if($total > 0 by if($line > 0 in your input field in name ="parameter" with method="GET" in your form

  • made an Edit in the post, and this bringing me that image !

  • Bring me the form to analyze, I will make a test here in my server.

  • <method="GET"> <label id="txt">Search for something:</label> <input id="bar" type="text" name="parameter" placeholder="Search"/> <input class="boot" type="Submit" value="Search"></a> <input class="boot" type="Submit" value="Return"/> </form>

  • I tried and it worked, look at your database like this table, the table fields can not contain characters nor accents nor spaces, just use the words without characters and instead of space use underline for example id_funcionario in your table echo this $line['Employee ID'] with spaces should be why it is not returning the values check why here is.

  • that’s exactly it, I didn’t pay attention to the table! Thank you very much colleague. Hugs !

  • Dispose any doubt of a salve!!!

Show 3 more comments

0

Try using a for, like this:

<?php
if($total){

    for ($i=0; $i < $total-1; $i++) { 
        ?>
        <tr>
            <td><?php echo $linha[$i]['ID Matrícula'] ?></td>
            <td><?php echo $linha[$i]['Funcionário'] ?></td>
            <td><?php echo $linha[$i]['Cargo'] ?></td>
            <td><?php echo $linha[$i]['Alocado'] ?></td>
        </tr>
        <?php 
    }
    mysqli_free_result($dados);}

    mysqli_close($mysqllink);
    ?>

I believe the Iterative variable (in this case $i) has to be used to identify which line is being printed

-1

I think the mistake is in the LIKE. Try to replace "SELECT * FROM funcio WHERE funcionario LIKE '%$parametro%' ORDER BY id_matricula" for "SELECT * FROM funcio WHERE funcionario='%$parametro%' ORDER BY id_matricula"

  • D4rck G4m3r the like would be to bring the results that contain the parameter, try not to answer if you are not sure. From a look: https://pt.meta.stackoverflow.com/

  • Sorry if I’m trying to help and I’ve never seen a SELECT with LIKE and only with =. https://dev.mysql.com/doc/refman/8.0/en/select.html

Browser other questions tagged

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