Jump to content

Search Code - Giving Column Name Keywords?


justlukeyou

Recommended Posts

I have some search code which I have been struggling with so I added a or die(mysql_error()); and it came back with the following message:

 

SELECT `description`, `fulldescription` FROM `productdbase` WHERE `keywords` LIKE '%tokyo%'Unknown column 'keywords' in 'where clause'

 

So I take it I am calling the column name 'keywords' where I as I should be calling it 'description'.  I have tried around 5 or 6 variations but I cant see where its called keywords.  Im confused as to what is call it keywords.

 

<?php
if (isset($_POST['keywords'])){
$keywords = mysql_real_escape_string (htmlentities(trim($_POST['keywords'])));
}

$errors = array();

$results = null;

if (empty($keywords)) {
$errors[] = 'Please enter a search term';
} else if (strlen($keywords)<3) {
$errors[] = 'Your search must be three or more characters';
} else if (($results = search_results($keywords)) === false) {
$errors[] = 'Your search for '.$keywords.' returned no results';
}

if (empty($errors))  {
foreach($errors as $error) {
echo $error, '</br>';
}


search_results ($keywords);

} else{
foreach($errors as $error) {
echo $error, '</br>';
}
}
    ?>


<?php
function search_results ($keywords) {
$returned_results = array();
$where = "";

$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);

foreach($keywords as $key=>$keyword) {
$where .= "`keywords` LIKE '%$keyword%'";
if ($key != ($total_keywords - 1)) {
$where .= " AND ";

}
}

$query_string = "SELECT `description`, `fulldescription`  FROM `productdbase` WHERE $where";
echo $query_string;

$query = mysql_query($query_string) or die(mysql_error());


$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;




if ($results_num === 0) {
return false;
}else{

echo 'something.';

}
}
?>

Link to comment
Share on other sites

What I have done now is to add a keywords column to my database and add keywords to the query and it still not reading the test keywords which I have added to the DB.

 

$query_string = "SELECT `keywords`, `description`, `fulldescription`  FROM `productdbase` WHERE $where";

 

Its strange, even when add a keywords column to test its still not working.

Link to comment
Share on other sites

foreach($keywords as $key=>$keyword) {

$where .= "`keywords` LIKE '%$keyword%'";

if ($key != ($total_keywords - 1)) {

$where .= " AND ";

 

You're the one who is saying to search the keywords column.

Link to comment
Share on other sites

Okay so I changed it to:

 

foreach($keywords as $key=>$keyword) {

$where .= "`description` LIKE '%$keyword%'";

if ($key != ($total_keywords - 1)) {

$where .= " AND ";

 

and it came back with this:

 

SELECT `keywords`, `description`, `fulldescription` FROM `productdbase` WHERE `description` LIKE '%tokyo%'Your search for tokyo returned no results

 

I take it im changing the wrong part.

Link to comment
Share on other sites

No, you changed the right part.

 

Next problem:

 

$query_string = "SELECT `description`, `fulldescription`  FROM `productdbase` WHERE $where";
echo $query_string;

$query = mysql_query($query_string) or die(mysql_error());

$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;

 

You are running the query, then getting the number of results based on another string: $results which does not exist.

 

You will always get 0.

Link to comment
Share on other sites

Why would you pick $query_results? Is that defined anywhere?

 

Look at the code and read it as if it's a sentence. Does $query_results make sense to use anywhere?

The entire last line makes no sense anyway.

$results_num = mysql_num_rows($query); would return 0 if it's 0, you don't need a complicated ternary that makes no sense.

Link to comment
Share on other sites

OMG Im so stressed about this.  Its really getting to me.

 

But the amazing thing is I just changed one thing which I cant even remember and its just returned "something.something." which is exactly what it should return.  Even the test column works.

 

And whats shocked me even me even more, when I enter 'toad' which isn't in my db it returns Your search for toad returned no results.

 

Im so pleased I think I just wet myself.

 

Thank you ever so much guys.

Link to comment
Share on other sites

Oh this is killing me.  It no longers, I tried to remove the query string and now it returns 'Query was empty'.

 

Could someone please kindly point out where its going wrong.  You know those times when you get incredibly stressed, im incredibly stressed lol

 

if (isset($_POST['keywords'])){
$keywords = mysql_real_escape_string (htmlentities(trim($_POST['keywords'])));
}

$errors = array();

$results = null;

if (empty($keywords)) {
$errors[] = 'Please enter a search term';
} else if (strlen($keywords)<3) {
$errors[] = 'Your search must be three or more characters';
} else if (($results = search_results($keywords)) === false) {
$errors[] = 'Your search for '.$keywords.' returned no results';
}

if (empty($errors))  {
foreach($errors as $error) {
echo $error, '</br>';
}


search_results ($keywords);

} else{
foreach($errors as $error) {
echo $error, '</br>';
}
}
    ?>


<?php
function search_results ($keywords) {
$returned_results = array();
$where = "";

$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);

foreach($keywords as $key=>$keyword) {
$where .= "`description` LIKE '%$keyword%'";
if ($key != ($total_keywords - 1)) {
$where .= " AND ";

}
}

$results = "SELECT `description`, `fulldescription`  FROM `productdbase` WHERE $where";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;

$query = mysql_query($query_string) or die(mysql_error());


$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;




if ($results_num === 0) {
return false;
}else{

echo 'something.';

Link to comment
Share on other sites

Okay so I have removed this part " ? mysql_num_rows($results) : 0" and now it returns the following:

 

So now I need to shop echoing the query?

 

SELECT `description`, `fulldescription` FROM `productdbase` WHERE `description` LIKE '%clearance%'something.SELECT `description`, `fulldescription` FROM `productdbase` WHERE `description` LIKE '%clearance%'something.

 

With this should I just show echo something?

 

if ($results_num === 0) {

return false;

}else{

 

echo 'something.';

 

}

Link to comment
Share on other sites

I could SCREAM

 

Now when I enter 'dippledopple' it still returns something. something.

 

jesirose, is incredibly appreciated but I just dont understand what you mean.  Could you please explain a bit further what the issues.

 

Im going round in big circles with this.

 

Once again, thanks for all your help.

Link to comment
Share on other sites

 

 

<?php
if (isset($_POST['keywords'])){
$keywords = mysql_real_escape_string (htmlentities(trim($_POST['keywords'])));
}

$errors = array();

$results = null;

if (empty($keywords)) {
$errors[] = 'Please enter a search term';
} else if (strlen($keywords)<3) {
$errors[] = 'Your search must be three or more characters';
} else if (($results = search_results($keywords)) === false) {
$errors[] = 'Your search for '.$keywords.' returned no results.  Please place another search.';
}

if (empty($errors))  {
foreach($errors as $error) {
echo $error, '</br>';
}


search_results ($keywords);

} else{
foreach($errors as $error) {
echo $error, '</br>';
}
}
    ?>


<?php
function search_results ($keywords) {
$returned_results = array();
$where = "";

$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);

foreach($keywords as $key=>$keyword) {
$where .= "`description` LIKE '%$keyword%'";
if ($key != ($total_keywords - 1)) {
$where .= " AND ";

}
}

$query_string = "SELECT `description`, `fulldescription`  FROM `productdbase` WHERE $where";
echo $query_string;

$query = mysql_query($query_string) or die(mysql_error());


$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;




if ($results_num === 0) {
return false;
}else{

echo 'something.';

}
}
?>

Link to comment
Share on other sites

Oh how stupid could I be, when I use this code without the query string it works.  At least it appears to.  British people sometimes!

 

<?php
if (isset($_POST['keywords'])){
$keywords = mysql_real_escape_string (htmlentities(trim($_POST['keywords'])));
}

$errors = array();

$results = null;

if (empty($keywords)) {
$errors[] = 'Please enter a search term';
} else if (strlen($keywords)<3) {
$errors[] = 'Your search must be three or more characters';
} else if (($results = search_results($keywords)) === false) {
$errors[] = 'Your search for '.$keywords.' returned no results.  Please enter another search.';
}

if (empty($errors))  {
foreach($errors as $error) {
echo $error, '</br>';
}


search_results ($keywords);

} else{
foreach($errors as $error) {
echo $error, '</br>';
}
}
    ?>


<?php
function search_results ($keywords) {
$returned_results = array();
$where = "";

$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);

foreach($keywords as $key=>$keyword) {
$where .= "`description` LIKE '%$keyword%'";
if ($key != ($total_keywords - 1)) {
$where .= " AND ";

}
}

$results = "SELECT `keywords`, `description`, `fulldescription`  FROM `productdbase` WHERE $where";



$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;




if ($results_num === 0) {
return false;
}else{

echo 'something.';

}
}
?>

Link to comment
Share on other sites

Yeah its definately working now.  It put in 'fulldescription' into here it will only search the fulldescription column.

 

foreach($keywords as $key=>$keyword) {

$where .= "`fulldescription` LIKE '%$keyword%'";

if ($key != ($total_keywords - 1)) {

$where .= " AND ";

 

However, if I do this

 

foreach($keywords as $key=>$keyword) {

$where .= "`fulldescription' , `description'  LIKE '%$keyword%'";

if ($key != ($total_keywords - 1)) {

$where .= " AND ";

 

it wont search anything. Can I get it two search 2 columns?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.