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. :)

It seems that you haven't initialised $search and are relying on register_globals being on - this is bad practice.

 

Try using $_POST['search'] instead of just $search - and tell your host to turn register_globals OFF, it's pure evil.

<?
//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....

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']
) ");

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.