Search engine

Alles, was PHP betrifft, kann hier besprochen werden.

Search engine

Postby multipianista » 08. February 2014 17:40

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>&nbsp; </p> No se ha encontrado ningún resultado que coincida con su búsqueda. ";
}
} else {
$_POST["datos"] = "";
}
?>
multipianista
 
Posts: 10
Joined: 24. January 2014 23:51
Operating System: Windows 8

Re: Search engine

Postby Altrea » 08. February 2014 18:33

Hi,

Code: Select all
SELECT * FROM datos ".$datos." ORDER BY titulo


After concatenating your full SQL-Statement echo it out as plain text before you execute it.
That way you can find syntax issues easily.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 8299
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64

Re: Search engine

Postby JJ_Tagy » 08. February 2014 18:45

You have a syntax error because you tie your WHERE clause to exactly the first entry. When the first entry is shorter than 4, you ignore it which ignores the WHERE clause.

FYI: Your else $datos = $datos block is useless.
JJ_Tagy
 
Posts: 678
Joined: 30. January 2012 13:44
Operating System: Windows 10 Pro x64

Re: Search engine

Postby multipianista » 08. February 2014 21:24

Thanks JJ_Tagy, it was the error, now is correct.

(The else $datos was a try to correct it that i forgot to delete)
multipianista
 
Posts: 10
Joined: 24. January 2014 23:51
Operating System: Windows 8

Re: Search engine

Postby Nobbie » 08. February 2014 21:58

Sorry for that, but your code is really horrible.

This statement

Code: Select all
else{$datos = $datos;}


is a pain and it does NOT correct anything.

At next, it is horribly wrong to assign any value to request variables, so this statement

Code: Select all
$_POST["datos"] = "";


is a real pain as well.
Nobbie
 
Posts: 8780
Joined: 09. March 2008 13:04

Re: Search engine

Postby Altrea » 08. February 2014 22:56

There are many possibilities for improvements like don't use the old mysql_* functions anymore, because they will be removed in one of the next major releases of PHP.
Use mysqli or PDO instead.

Don't use inline styling, use CSS instead.

But everybody have to start somewhere. So keep going :D
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 8299
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64


Return to PHP

Who is online

Users browsing this forum: No registered users and 3 guests