Forum Moderators: coopster

Message Too Old, No Replies

Query Help

         

puckparches

12:52 am on Sep 26, 2006 (gmt 0)

10+ Year Member Top Contributors Of The Month



I need to compare 3 fields to one variable. Return a row if one of the 3 fields in that row is equal to the variable.
So I did this:

SELECT *
FROM table
WHERE cat_y LIKE '%".$catid."%' ¦¦ cat_x LIKE '%".$catid."%' ¦¦ cat_z LIKE '%".$catid."%'

But the browser turn of by it self if I run the query. What am doing wrong?

Thanks

eelixduppy

12:55 am on Sep 26, 2006 (gmt 0)



>>>But the browser turn of by it self if I run the query.

Seems like you have a fatal error. Would you mind posting some of the code? Also, adding error_reporting [us2.php.net](E_ALL); at the top of your script may shed some light.

puckparches

1:07 am on Sep 26, 2006 (gmt 0)

10+ Year Member Top Contributors Of The Month



<quote>

$catid = $_POST['catid'];
$subcatid = $_POST['subcatid'];

/* Set current, prev and next page */

$page = (!isset($_GET['page']))? 1 : $_GET['page'];
$prev = ($page - 1);
$next = ($page + 1);

/* Max results per page */

$max_results = 8;

/* Calculate the offset */

$from = (($page * $max_results) - $max_results);

SELECT *
FROM table
WHERE cat_y LIKE '%".$catid."%' ¦¦ cat_x LIKE '%".$catid."%' ¦¦ cat_z LIKE '%".$catid."%'
AND subcat_a LIKE '%".$subcatid."%'

</quote>

Not to much of code, just that for the pagination. An then I runt the same query to get the rows information.

If I remove this part: ¦¦ cat_x LIKE '%".$catid."%' ¦¦ cat_z LIKE '%".$catid."%'

All is working OK, but I had to add a couple of fields for my program.

Thank you for your help.

eelixduppy

1:24 am on Sep 26, 2006 (gmt 0)



I'm assuming your query is properly sent to the db?:

$catid = mysql_real_escape_string($catid);
$subcatid = mysql_real_escape_string($subcatid);
$query = "SELECT * FROM table WHERE cat_y LIKE '%".$catid."%' ¦¦ cat_x LIKE '%".$catid."%' ¦¦ cat_z LIKE '%".$catid."%' AND subcat_a LIKE '%".$subcatid."%'";
$result = mysql_query($query) or die([url=http://us2.php.net/manual/en/function.mysql-error.php]mysql_error[/url]());

Make sure that one, setting error_reporting to E_ALL doesn't return any errors. Two, that your query actually works. Try your query in the command line if you need validation. mysql_error() will also tell you if anything is wrong with the query.

I hope this has helped some!

puckparches

1:52 am on Sep 26, 2006 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thank you for your help:

It keeps dieing (my browser), but if I change:

From: cat_y LIKE '%".$catid."%' ¦¦ cat_x LIKE '%".$catid."%' ¦¦ cat_z LIKE '%".$catid."%'

To: WHERE (cat_y ¦¦ cat_x ¦¦ cat_z) LIKE '%".$catid."%'

The browser doesn't die, but I get this error:

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 '¦¦ cat_x ¦¦ cat_z) LIKE '%1%'
AND subcat_a LIKE '%1%'
AND zip LIKE '%91401%'
' at line 3

Thank you!

Is there any other way you think I can compare these 3 fields to my variable?

eelixduppy

3:00 am on Sep 26, 2006 (gmt 0)



Try making your query this:

$query = "SELECT * FROM table WHERE ((cat_y = '".$catid."') ¦¦ (cat_x = '".$catid."') ¦¦ (cat_z = '".$catid."')) AND subcat_a = '".$subcatid."'";

By the looks of it, you want to check for equality using '=', not LIKE with wildcards, however this is just an assumption.

[added]Make sure you replace the pipe character(¦)if you are going to copy and paste. WebmasterWorld breaks these![/added]

[edited by: eelixduppy at 3:20 am (utc) on Sep. 26, 2006]

lmo4103

3:14 am on Sep 26, 2006 (gmt 0)

10+ Year Member



Is it where you have to set something in mysql to get the ¦¦ to do concat?

puckparches

3:45 am on Sep 26, 2006 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thank You! Thank you!

Finally it worked. I did what eelixduppy suggested and is working perfect.

Thank you again guys for your help and patience.