LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-26-2003, 10:40 AM   #1
markus1982
Senior Member
 
Registered: Aug 2002
Location: Stuttgart (Germany)
Distribution: Debian/GNU Linux
Posts: 1,467

Rep: Reputation: 46
php, sql and query with a lot of or's in where clause


I've PHP (in combination with PEAR) code like the following:
Code:
 $tmp_ram['motherboard_cpu_ram'] = $webdb->getAll(
 		  "SELECT"
		. "		ram_frequence"
		. " FROM"
		. "		motherboard_cpu_ram"
		. " WHERE"
		. "		motherboard = '".mysql_real_escape_string($pc_data['motherboard']['product_number'])."'"
		. " AND"
		. "		cpu_fsb = '".$pc_data['cpu']['data']->fsb."'"
 	);

 for ($i = 0; $i < sizeof($tmp_ram['motherboard_cpu_ram']); $i++) {
	$tmp_ram['motherboard_ram'] = $webdb->getAll(
			  "SELECT"
			. "		module_max,"
			. "		ports_ss,"
			. "		ports_ds"
			. " FROM"
			. "		motherboard_ram"
			. " WHERE"
			. "		motherboard = '".mysql_real_escape_string($pc_data['motherboard']['product_number'])."'"
			. " AND"
			. "		frequence = '".$tmp_ram['motherboard_cpu_ram'][$i]->ram_frequence."'"
		);
		
	for ($x = 0; $x < sizeof($tmp_ram['motherboard_ram']); $x++) {
		$tmp_ram['ram'] = $webdb->getAll(
				  "SELECT"
				. "		product_number"
				. "	FROM"
				. "		ram"
				. " WHERE"
				. "		frequence = '".$tmp_ram['motherboard_cpu_ram'][$i]->ram_taktfrequence."'"
				. " AND"
				. "		modulsize <= '".$tmp_ram['motherboard_ram'][$x]->module_max."'"
			);

		for ($n = 0; $n < sizeof($tmp_ram['ram']); $n++) {
			$tmp_ram['product_numbers_tmp'][] = $tmp_ram['ram'][$n]->product_number;
		}
	}
 }

 // remove duplicate entries
 $tmp_ram['product_numbers'] = array_unique($tmp_ram['product_numbers_tmp']);
$tmp_ram['product_numbers'] is an array with the product numbers of the compatible articles. So I could select those using for instance:
Code:
SELECT * FROM products WHERE product_number = 1 OR product_number = 2
However this looks pretty weird with like 20 OR's - don't you aggree? Does anybody know a better solution that with the OR's?

Awaiting your answers :-)
 
Old 09-26-2003, 12:27 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
You can do something like this:

Code:
select * from product where product_number in ( 1, 2, 3, 4 );
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Query MS SQL from remote php simon@saq.co.uk Linux - General 2 10-17-2005 09:44 AM
Question: How to define an SQL HAVING clause in relational algebra? jdruin Linux - Software 1 11-08-2004 07:50 AM
SQL query help pls. vickr1z Programming 8 10-18-2004 11:25 PM
Massive SQL Query patpawlowski Programming 7 03-05-2004 04:24 PM
SQL Query question oulevon Programming 7 01-16-2004 01:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration