Searching for a surname in a field with 2 surnames in Mysql

1

I have a table with 4 fields: No tuition , Last name , Names and State .

>

I want to do a search on the surname and if for example I search only those that are named Velez, I list the information of all those who are named Velez.

I have this form

<form name="consulta2" method="post" action="resultado1.php"> 
        <input placeholder="Buscar por Apellido Paterno:" type="text" name="busca" maxlength="25" class="input-search"><br/>
        <input type="submit" id="submit" value="Buscar">
        </form>

And the process is in:

$busca = $_POST['busca'];

$sql = "SELECT coleap, colenom, estado FROM psicologos WHERE coleap LIKE %$busca% "; 

echo $sql;

$result = mysql_query($sql);
//se despliega el resultado  
echo "<div align='center'>";
echo "<table style='border:1px solid #CCCCCC; width:80%';>";
echo "<tr>";
echo "<th style='font-family:Arial; border-right:1px solid #ccc; text-align:center; border-bottom:1px solid #ccc;  font-size:13px'>Apellidos</th>";
echo "<th style='font-family:Arial; border-right:1px solid #ccc; text-align:center; border-bottom:1px solid #ccc;  font-size:13px'>Nombres</th>";
echo "<th style='font-family:Arial; border-right:1px solid #ccc; text-align:center; border-bottom:1px solid #ccc;  font-size:13px'>Estado</th>";
echo "</tr>";
while ($row = mysql_fetch_row($result)) {
    echo "<tr>";
    echo "<td style='font-family:Arial; border-right:1px solid #ccc; text-align:center; font-size:13px'>" . $row[0] . "</td>";
    echo "<td style='font-family:Arial; border-right:1px solid #ccc; text-align:center; font-size:13px'>" . $row[1] . "</td>";
    echo "<td style='font-family:Arial; border-right:1px solid #ccc; text-align:center; font-size:13px'>" . $row[2] . "</td>";

    echo "</tr>";
}
echo "</table>";
  

But it does not work for me, or it does not show me anything or it shows me the whole list   without any filter What am I wrong?

    
asked by John Mendoza Rojas 03.04.2018 в 22:14
source

2 answers

1

You can use the SOUNDS LIKE FROM MySQL operator; as follows

SELECT * FROM tabla_name WHERE apellido SOUNDS LIKE "Velez";
  

That will help you find a match for the text string that   you write, as long as they are similar, also helping you with the LIKE operator

Example with the operator %% and LIKE

  • The% operator used at the end of the text string will search for the match at the beginning of the text string
  • The% operator used at the beginning of the text string will search for the match at the end of the text string
  • Wildcards %%, are used when you want to find one or more matches within the text or numeric string to be compared

      

    Or you can also use the percentage symbol operator to   find matches in the text string you pass %%

    SELECT * FROM tabla_name WHERE apellido LIKE "Velez%";
    
    SELECT * FROM tabla_name WHERE apellido LIKE "%Velez";
    
        
    answered by 04.04.2018 в 00:19
    0

    put %busca% in single quotes.

    $sql = "SELECT coleap, colenom, estado FROM psicologos WHERE coleap LIKE '%$busca%' ";
    
        
    answered by 04.04.2018 в 00:02