Jump to content

[SOLVED] Search Problem


cs1h

Recommended Posts

Hi,

 

I'm not very good with php and can't see whats wrong with the following piece of code

 

$sql = "SELECT * FROM items WHERE (country='" . mysql_real_escape_string($targetb)" OR continent='" . mysql_real_escape_string($targetb) . "') AND type='" . mysql_real_escape_string($_POST['Type']) . "' AND (Abstract LIKE '%$keyword%' OR town LIKE '%$keyword%') ORDER BY id DESC";

 

Does anyone know why it doesn't work

 

Cheers

Colin

Link to comment
Share on other sites

A better looking and simple to debug code should be:

 

$targetb = mysql_real_escape_string($targetb); //or post or get data or whatever
$type = mysql_real_escape_string($_POST['Type']);

$sql = "SELECT * FROM items WHERE (country='$targetb' OR continent='$targetb') AND type='$type' AND (abstract LIKE '%$keyword%' OR town LIKE '%$keyword%') ORDER BY id DESC";

$results = mysql_query($sql) or die(mysql_error());

 

Note the "or die(mysql_error())", it should bring up the error caused. See what error ure getting.

Link to comment
Share on other sites

Hi,

 

I changed it to

 

$targetb = $_POST['menuFilesDMA'];
$targetb = str_replace(' ','_', $targetb);
$targetb = mysql_real_escape_string($targetb);

$type = mysql_real_escape_string($_POST['Type']);

mysql_connect("localhost","adder","clifford"); 

mysql_select_db("real") or die("Unable to select database"); 

$keywords = preg_split("/[\s,]+/", trim($_POST['keyword']));

$sql = "SELECT * FROM items WHERE (country='$targetb' OR continent='$targetb') AND type='$type' AND (abstract LIKE '%$keyword%' OR town LIKE '%$keyword%') ORDER BY id DESC";

$result = mysql_query($sql) or die(mysql_error());

 

and now I get the error,

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in D:\Inetpub\vhost\myroho.com\httpdocs\absearch.php on line 5

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in D:\Inetpub\vhost\myroho.com\httpdocs\absearch.php on line 5

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in D:\Inetpub\vhost\myroho.com\httpdocs\absearch.php on line 7

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in D:\Inetpub\vhost\myroho.com\httpdocs\absearch.php on line 7

 

Any ideas why this is,

 

Thanks for all the help,

 

Colin

Link to comment
Share on other sites

Move the following lines under the mysql connect statement i.e,

 

mysql_connect("localhost","adder","clifford");

mysql_select_db("real") or die("Unable to select database");

 

$targetb = mysql_real_escape_string($targetb);

$type = mysql_real_escape_string($_POST['Type']);

 

This should solve your problem.

 

good luck.

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.