Jump to content

PHP and MYSQL Search - referencing a variable properly...


vickree

Recommended Posts

Hello,

 

I've created a search using fulltext mysql techniques - and everything is dandy on my personal server - where I test everything. When I transfered it to the server here at my work place - the $search isn't getting substituted correctly... Is this a common error among different versions of PHP on different servers?

 

The code I use is:

 

<!-- Start of Search Function --><br>
<br>
Please type the name of the product you would like to find.
<form method="post" action="search_test.php">
<input type="text" size="30" maxlength="28" name="search">
<input name="submit" type="submit" value="SEARCH!">
</form>
<!-- end of search -->
<?
//Pulling the results for matching words
$result = mysql_query ("SELECT image, location, itemName, price
FROM `tj_search`
WHERE MATCH (
details
)
AGAINST (
'$search'
) ");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf(

"<br><img src=%s/> - <a href=%s> %s</a> - $ %s

<br>", $row[0], $row[1], $row[2], $row[3]);
}

mysql_free_result($result);
?>

 

Again - as I mentioned - this works FINE on my server - but not on the one at work.

I ran a test to see if $search is being substituted... but it comes blank.

 

If I change the

 

AGAINST (

'$search'

) ");

 

to one of the keywords - such as uniforms - ALL the uniforms will come up. So it has nothing to do with displaying. Just referencing the variable and pulling the items where the word matches the keywords in the MYSQL table.

 

ANY HELP IS MUCH APPRECIATED. :)

Link to comment
Share on other sites

<?
//Pulling the results for matching words
$result = mysql_query ("SELECT image, location, itemName, price
FROM `tj_search`
WHERE MATCH (
details
)
AGAINST (
$_POST['search']
) ");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf(

"<br><img src=%s/> - <a href=%s> %s</a> - $ %s

<br>", $row[0], $row[1], $row[2], $row[3]);
}

mysql_free_result($result);
?>

 

Im getting an error on like 110 - which is $_POST['search']

 

Am sure im probably missing something completely stupid....

Link to comment
Share on other sites

When including a variable in a string, you should enclose it in {}. If the variable is an array element, you must enclose it. Also, you removed the quotes around the match term, which is an SQL string and needs the quotes.

 

So perhaps

AGAINST (
'{$_POST['search']}'
) ");

 

Instead of

AGAINST (
$_POST['search']
) ");

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.