Jump to content

Double Running Query?!


justlukeyou

Recommended Posts

Hi,

 

I have written some code however when I echo the query it runs it twice...

 

SELECT `description`, `fulldescription` FROM `productdbase` WHERE `keywords` LIKE '%table%'something found.SELECT `description`, `fulldescription` FROM `productdbase` WHERE `keywords` LIKE '%table%'something found.

 

I only have the query once in my code. Any suggesions?

 

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

$errors = array();

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 (search_results($keywords) === false) {
$errors[] = 'Your search for '.$keywords.' returned no results';
}

if (empty($errors)) {

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);



if ($results_num === 0) {
return false;
}else{
echo 'something found.';
}




}
?>

Link to comment
Share on other sites

It's easy .... See comments.

 

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

$errors = array();

// the results variable
$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) {   // get the results
$errors[] = 'Your search for '.$keywords.' returned no results';
}

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

Link to comment
Share on other sites

Many thanks, I entered in those three points and it returns with the same result.

 

If run the query instead of echoing it still doesn't read my database.  For example I know I have table in database.

 

Your search for table returned no results

 

Is there anyway I echo errors out which tell me why it is not reading the db?

 

Link to comment
Share on other sites

Hi,

 

I ran the following:

"SELECT `description`, `fulldescription`  FROM `productdbase` WHERE table"

 

And this is the response I got..

 

#1064 - 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 '"SELECT `description`, `fulldescription` FROM `productdbase` WHERE table"' at line 1

 

Im not familiar with running queries directly in mysql, how do I search a keyword if I am posting it from another?

Link to comment
Share on other sites

Hi,

 

This is it

 

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

 

This is the result:

 

#1064 - 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 '$query_string = "SELECT `description`, `fulldescription` FROM `productdbase` WH' at line 1

 

Does it mean anything to you?

Link to comment
Share on other sites

Hi,

 

This is the result I got from running the following query:

 

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

 

QUERY:

 

$query_string = "SELECT `description`, `fulldescription`  FROM 'productdbase' WHERE $where";
echo $query_string;
$query = mysql_query($query_string);

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.