Page 1 of 1

Select from multiple collum from table in db

Posted: Mon Oct 19, 2009 12:37 pm
by FearKiller
Hi all. :banghead: my problem is this:
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 'desc LIKE '%video%' OR title LIKE '%video%' OR keywords LIKE '%share%' OR desc L' at line 1

it is for my SE (search engine)
i using php and mysql 2 years but this is first time when i need to select data from multiple collum.

This is my code:

Code: Select all

 
<html>
<head>
</head>
<body>
<?php 
$search = $_GET["s"];
?>
 
        <form action="" method="get">
              <font face="sans-serif" size="5">
              <center>
              Doyo - Search Engine project<br>
                <input type="text" size="50" name="s" value="<?php echo $search; ?>">
                <input type="submit" name="submit" value="Search">
              </center>
              </font>
<object><hr height="2"></object>
 
<?php
if(!$search){
}else{
 
if(strlen($search)<=2){
  Echo "Entered keyword is too short. Keyword can not be shorter then two letters.";
}else{
 
 
 
$search_explode = explode(" ", $search);
foreach($search_explode as $search_each){
$x++;
if($x == 1){
$construct .= "keywords LIKE '%$search_each%' OR desc LIKE '%$search_each%' OR title LIKE '%$search_each%'";
}else{
$construct .= " OR keywords LIKE '%$search_each%' OR desc LIKE '%$search_each%' OR title LIKE '%$search_each%'";
}
}
mysql_connect("mysql.ic.cz", "MYDB", "MYPASS") or die(mysql_error());
mysql_select_db("MYDB") or die(mysql_error());
 
$run = "SELECT * FROM links WHERE $construct";
$result = mysql_query($run) or die(mysql_error());
 
while($getrow = mysql_fetch_assoc($result)){ 
$title = $getrow[title];
$link = $getrow[link];
$desc = $getrow[desc];
 
 
echo $title."<br>";
echo $desc."<br>";
echo "<a href="$link">$link</a><br><br>";
}
 
 
}
 
}
?>
</body>
</html>
Thanks for help :D :D :D :D

Re: Select from multiple collum from table in db

Posted: Mon Oct 19, 2009 12:41 pm
by nshiell
Dude add

Code: Select all

var_dump($run);
to see what the SQL your making is like, and post it in this forum if u still are having issues.

Re: Select from multiple collum from table in db

Posted: Mon Oct 19, 2009 2:52 pm
by FearKiller
hmm. no change.

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 'desc LIKE '%share%' OR title LIKE '%share%'' at line 1

Code: Select all

 
mysql_connect("mysql.ic.cz", "user", "pw") or die(mysql_error());
mysql_select_db("ic_getvideo") or die(mysql_error());
$search = $_GET["s"];
$search_explode = explode(" ", $search);
foreach($search_explode as $search_each){
$x++;
if($x == 1){
$construct .= "keywords LIKE '%$search_each%' OR desc LIKE '%$search_each%' OR title LIKE '%$search_each%'";
}else{
$construct .= " OR keywords LIKE '%$search_each%' OR desc LIKE '%$search_each%' OR title LIKE '%$search_each%'";
}
}
 
$run = "SELECT * FROM links WHERE $construct";
$result = mysql_query($run) or die(mysql_error());
var_dump($result);
link to site http://getvideo.ic.cz
when i use:

Code: Select all

if($x == 1){
$construct .= "keywords LIKE '%$search_each%'";
}else{
$construct .= " OR keywords LIKE '%$search_each%'";
}
}
This works, but I need to find keywords from the title and desc and from keywords and not only keywords. :(
Other info.:
my db structure:
database ic_getvideo with table links and there are collum: id, url, title, desc, keywords

Re: Select from multiple collum from table in db

Posted: Mon Oct 19, 2009 3:18 pm
by califdon
First of all, you're not giving us the information that we might use to help you. Show us what your SQL string looks like just before you try to run your query!

Whenever you get a MySQL error message like that, it means that the error has occurred just before the part of the SQL that is quoted. Just knowing that, my first guess is that you are not getting a value for your first keyword, so your SQL string probably looks like:

Code: Select all

SELECT * FROM links WHERE keywords LIKE '%%' OR desc LIKE ... etc.

Re: Select from multiple collum from table in db

Posted: Tue Oct 20, 2009 3:01 pm
by FearKiller
The problem is solved :) Thanks for all :)

with this is working sql:

Code: Select all

 
SELECT * FROM links WHERE [b][color=#FF0000]`[/color][/b][color=#0000FF]keywords[/color][b][color=#FF0000]`[/color][/b] LIKE '%$search_each%' OR [color=#FF0000][b]`[/b][/color][color=#0000FF]title[/color][color=#FF0000][b]`[/b][/color] LIKE '%$search_each%' OR `desc` LIKE '%$search_each%'
 
and with this not working :D

Code: Select all

 
SELECT * FROM links WHERE [color=#0000FF]keywords[/color] LIKE '%$search_each%' OR [color=#0000FF]title[/color] LIKE '%$search_each%' OR [color=#0000FF]desc[/color] LIKE '%$search_each%'