View Single Post
Old 10-06-2012, 11:14 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,515
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
The query should work, put the sql into the sql tab in phpmyadmin, change the $id for a real value and see what happens. What won't work is your use of foreach().

foreach($list as $id), $id will be the index of the $list array (the position in the array - eg 1,2,3,4,5,6 etc)
foreach($list as $key => $value), $value will be what you want while $key will be the index

You shouldn't need to run the query in a loop though, that could end up running hundreds or even thousands of queries on the sql server. On a shared host that could kill it!

Instead, something like this would be better - adjust the SQL query in a loop and then run it just once:
PHP Code:
$Where implode(' or id = '$list);

//SELECT * FROM SERVERS WHERE id = 1 or id = 2 or id = 3 etc
$Query "SELECT * FROM SERVERS WHERE id = $Where";

//Run query here, no looping queries, no sql server hogging etc. 
That assumes that the values of $list are all numerical. If they are string based you'll need to use singles quotes in the sql.

If your id is not numerical (eg say it has letters in it) then this would do it instead:
PHP Code:
foreach($list as $key => $value)
   {
   
$list[$key] = "'$value'"//Note use of single quotes
   
}


$Where implode(' or id = '$list);

//SELECT * FROM SERVERS WHERE id = 'a1' or id = 'a2' or id = 'a3' etc
$Query "SELECT * FROM SERVERS WHERE id = $Where"
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 10-06-2012 at 11:18 PM..
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
alphamale (10-06-2012)