Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-16-2013, 12:32 PM   PM User | #1
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 76
Thanks: 61
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
Looping through an array and running a query each time?

Hello, I'm looking for some advice if possible please. I have an array to loop through

PHP Code:
$people = array (
'John',
'Mark',
'Joe'
);
//and so on 
Now for each element in the array I use a foreach through and run a query on my database table so

Code:
Select id from table where name = '$person'
Now With each query there will be a number of 'id' values output. What I want to do is hold these values in an array

Doing this seems to also hold the values from each previous query in the foreach as well

PHP Code:
foreach($people as $person) {
$query "Select id from table WHERE name = '$person'";
$result mysql_query($query);
  while(
$row mysql_fetch_array($result)) {
    
$ids[] = $row;
    
print_r($ids);

Can anyone help me here please? Thank you
Oatley is offline   Reply With Quote
Old 01-16-2013, 02:07 PM   PM User | #2
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
You might want to change it to this:

PHP Code:
foreach($people as $person) {
$query "Select id from table WHERE name = '$person'";
$result mysql_query($query);
  while(
$row mysql_fetch_array($result)) {
    
$ids[$person][] = $row;
    
print_r($ids);

This should create an array like this...

Code:
$person = array(
    'John' => array( //all john entries here), 
    'Mark' => array( //all mark entries here),
    'Joe' => array ( //all joe entries here)
)
Of course, it depends on how you want to actually use that data.

You could also simply run the query like this:

PHP Code:
$query "Select id from table WHERE name IN ('".implode("','",$people)."'"
Which would get you all the data in 1 query.
bcarl314 is offline   Reply With Quote
Users who have thanked bcarl314 for this post:
Oatley (01-17-2013)
Old 01-16-2013, 04:50 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Use the IN clause. Try to avoid looping queries as much as you can. Alternatively you can use prepared statements and loop the bound parameter (which only sends the parameter's back to the sql so communication wise it should be the same). Prepared statements IMO are much better approach, and since you'll need to convert to MySQLi or PDO soon anyways (Mysql library is now officially deprecated and will disappear in the future), I'd suggest doing it with prepared statements.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Oatley (01-17-2013)
Old 01-19-2013, 01:57 PM   PM User | #4
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 76
Thanks: 61
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
Thanks all I 'm going to look into using one query as advised, but for now I'm still after a little help on this though if that's OK please.

I'm still getting a litte confused on arrays. I thought i'd solved what I was trying to do based on the kind advice offered here, but it's not working.

If I have a simple array like so

PHP Code:
$people = array(
'John',
'Mark'
); 
Then for each element in the array I run through as kindly advised

PHP Code:
foreach($people as $person) {
 
$sql "Select id from table WHERE name = '$person'";
 
$result mysql_query($sql);
   while(
$row mysql_fetch_array($result)) {
     
$ids[$person][] = $row;
     
print_r($ids);
   }

It shows some results. But say John has 9 id's associated with his name, I get the first 'id' in one array, then that same id repeated in the next array with the next value as well and so on as per the example below

Code:
Array
(
  [John] => Array
       (
           [0] => Array
               (
                   [0] => 5066
                   [id] => 5066
               )

       )

)
Array
(
  [John] => Array
       (
           [0] => Array
               (
                  [0] => 5066
                  [id] => 5066
               )

           [1] => Array
               (
                   [0] => 5837
                   [id] => 5837
               )

       )


       )
When I want them all to be shown in the one 'John' array. Then show me the next set of results in one 'Mark' array?

Can anyone please offer any help? Thank you

Last edited by Oatley; 01-19-2013 at 02:01 PM..
Oatley is offline   Reply With Quote
Old 01-19-2013, 04:26 PM   PM User | #5
rgb
New Coder

 
Join Date: Jul 2011
Posts: 14
Thanks: 0
Thanked 2 Times in 2 Posts
rgb is an unknown quantity at this point
mysql_fetch_array() needs a second argument e.g MYSQL_NUM to return just one result row instead of both the associative and the numerical ones. You are also creating another array inside your while loop. Why not just something like:

PHP Code:
foreach($people as $person) {
 
$sql "Select id from table WHERE name = '$person'";
 
$result mysql_query($sql);
   while(
$row mysql_fetch_array($result,MYSQL_NUM)) {
    echo 
$person.$row[0];
   }

rgb is offline   Reply With Quote
Users who have thanked rgb for this post:
Oatley (01-19-2013)
Old 01-19-2013, 05:46 PM   PM User | #6
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 76
Thanks: 61
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
Thank you but I am looking to produce an array for each person holding the possible multiple id values for each iteration in my foreach here for 'John' and 'Mark' rather than echo the id and persons name each time. But thank you for the help.
Oatley is offline   Reply With Quote
Old 01-19-2013, 06:05 PM   PM User | #7
rgb
New Coder

 
Join Date: Jul 2011
Posts: 14
Thanks: 0
Thanked 2 Times in 2 Posts
rgb is an unknown quantity at this point
Same principle. Wouldn't this work?

PHP Code:
foreach($people as $person) { 
 
$sql "Select id from table WHERE name = '$person'"
 
$result mysql_query($sql); 
   while(
$row mysql_fetch_array($result,MYSQL_NUM)) { 
    
$ids[$person][] = $row[0];
    
//$person[]=$row[0]; this is wrong = it would overwrite each iteration!
   
   



Last edited by rgb; 01-19-2013 at 06:47 PM.. Reason: Got it wrong
rgb is offline   Reply With Quote
Old 01-19-2013, 06:07 PM   PM User | #8
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 45
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by Oatley View Post
Thank you but I am looking to produce an array for each person holding the possible multiple id values for each iteration in my foreach here for 'John' and 'Mark' rather than echo the id and persons name each time. But thank you for the help.
Well in that case you really do need to take notice of the replies given to you about not using multiple SQL calls inside a loop then.

If you're not prepared to act on the easier advice people have given you, they probably won't help you with the harder more complex version. Why do I say it's harder? - because the results you're trying to get are harder to obtain the way you're doing it.
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum 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.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:21 PM.


Advertisement
Log in to turn off these ads.