Hi, I have a proble with this code. If I write some words with more length than 3, then it works, (for example "Green tree") but when I write a word longer than 3 and other shorter ("The green tree") it says:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OR informacion LIKE '%green%' OR informacion LIKE '%tree%' ORDER BY titulo' at line 1
Here is the code and thanks for your help:
<?php
if(isset($_POST["datos"])){
$palabras = explode(" ",utf8_decode($_POST["datos"]));
$totalresults = count($palabras);
$datos = "";
for($i=0;$i < $totalresults;$i++){
$longitud = (($i > 0) ? " OR " : " WHERE ")."informacion LIKE '%".mysql_real_escape_string($palabras[$i])."%'";
if(strlen($palabras[$i])>3){$datos .= $longitud;}
else{$datos = $datos;}
}
$sacacanciones = mysql_query("SELECT * FROM datos ".$datos." ORDER BY titulo") or die(mysql_error());
if(mysql_num_rows($sacacanciones) > 0){
echo "<hr /> <table><tr style=font-weight:bold;background-color:#000;><td style=color:#fff;>Título</td><td style=color:#fff;>Autor</td><td style=color:#fff;>Instrumentos</td><td style=color:#fff;>Páginas</td><td style=color:#fff;>Descarga</td></tr>";
while($c = mysql_fetch_array($sacacanciones)){
echo "<tr>
<td>".$c["titulo"]."</td><td>".utf8_encode($c["autor"])."</td>
<td style=padding-left:0px;padding-right:0px;>".utf8_encode($c["instrumentos"])."</td>
<td>".utf8_encode($c["paginas"])."</td><td><a target='_blank' title='Descargar partitura' href=\"" .$c["descarga"]."\">Descargar</a></td>
</tr>";
}
echo "</table>";
} else {
echo "<hr /> <p> </p> No se ha encontrado ningún resultado que coincida con su búsqueda. ";
}
} else {
$_POST["datos"] = "";
}
?>