Search in different mysql table fields

0

I happen to be implementing a search engine, but I want it to be searched by entering different values, I mean, I have a table that has 3 fields student_name, student_and student_name, what I want is that when entering a number display field values rut or if you have a letter that you look for in the field name or surname and show them, so far I have this on the server side (php).

 $buscar = $_POST['b'];

  if(!empty($buscar)) {
        buscar($buscar);
  }

  function buscar($b) {
        $con = mysql_connect('localhost','root', '12345678');
        mysql_select_db('colegio_pool', $con);

        $sql = mysql_query("SELECT * FROM alumno WHERE nombre_alumno LIKE '%".$b."%' LIMIT 10" ,$con);

        $contar = @mysql_num_rows($sql);

        if($contar == 0){
              echo "No se han encontrado resultados para '<b>".$b."</b>'.";
        }else{
          while($row=mysql_fetch_array($sql)){
            $nombre = $row['nombre_alumno'];
            $rut = $row['rut_alumno'];

            echo "<a><p class='alert alert-info' style='text-align:center'>".$nombre."</p></a>";

        }
    }

}

, how can I do to implement what I want? thanks in advance

    
asked by Alexi Gallegos Perez 30.01.2018 в 18:06
source

1 answer

0

I do not know if I understood you very well, but I think what you need is that when you capture by $_POST the variable with which you are going to filter, look for you in the 3 columns of your table:

  • nombre_alumno
  • rut_alumno
  • apellido_alumno

If so, use this query:

$sql = mysql_query("SELECT * FROM alumno WHERE nombre_alumno LIKE '%".$b."%' OR rut_alumno LIKE '%".$b."%' OR apellido_alumno LIKE '%".$b."%' LIMIT 10" ,$con);

In this way, look for matches that are in multiple columns.

Edited

I have not been able to test this code since the version of php that I have is the 7.0 and the functions of mysql_connect are obsolete and I have not been able to install an older version.

Try this:

<?php
$buscar = $_POST['b'];

if(!empty($buscar)) {
    buscar($buscar);
}

function buscar($b) {
    $con = mysql_connect('localhost','root', '12345678');
    mysql_select_db('colegio_pool', $con);

    $sql = mysql_query("SELECT * FROM alumno WHERE nombre_alumno LIKE '%".$b."%' OR rut_alumno LIKE '%".$b."%' OR apellido_alumno LIKE '%".$b."%' LIMIT 10" ,$con);

    $contar = @mysql_num_rows($sql);

    if($contar == 0){
            echo "No se han encontrado resultados para '<b>".$b."</b>'.";
    }else{
        while($row=mysql_fetch_array($sql)){
        $nombre = $row['nombre_alumno'];
        $rut = $row['rut_alumno'];
        $apellido = $row['apellido_alumno'];
        if (strpos($nombre, $b)>=0)
            echo "<a><p class='alert alert-info' style='text-align:center'>nombre_alumno:".$nombre."</p></a>";
        else if (strpos($rut, $b) >= 0) 
            echo "<a><p class='alert alert-info' style='text-align:center'>rut_alumno: ".$rut."</p></a>";
        else if (strpos($apellido, $b) >= 0)
            echo "<a><p class='alert alert-info' style='text-align:center'>apellido_alumno: ".$apellido."</p></a>";

    }
}
    
answered by 30.01.2018 в 18:28