Select from multiple collum from table in db

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
FearKiller
Forum Newbie
Posts: 4
Joined: Sun May 24, 2009 2:39 pm

Select from multiple collum from table in db

Post 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
nshiell
Forum Newbie
Posts: 12
Joined: Mon Oct 19, 2009 12:20 pm

Re: Select from multiple collum from table in db

Post 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.
FearKiller
Forum Newbie
Posts: 4
Joined: Sun May 24, 2009 2:39 pm

Re: Select from multiple collum from table in db

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Select from multiple collum from table in db

Post 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.
FearKiller
Forum Newbie
Posts: 4
Joined: Sun May 24, 2009 2:39 pm

Re: Select from multiple collum from table in db

Post 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%'
 
Post Reply