Page 1 of 1

Newbie: 'Correct' way of retreiving MySQL data

Posted: Thu Jun 04, 2009 3:18 pm
by mmX
Seems like a billion different ways to skin this cat; and, I'm not sure what the most effective/best practice is. Just pulling basic information and displaying it from a database...

What I'm looking at:

Code: Select all

$supervisee = mysql_query("SELECT * FROM tblEmployee WHERE DEPARTMENT = '" . $Dept . "'");
 
while($results = mysql_fetch_array($supervisee)) {
     foreach ($results as $temp) {
          echo $temp . " ";
     }
     echo "<br />";
} 
That's simple and effective; but, I get 2 results for each entry. A numerical and associative index, example print_r output:

Code: Select all

Array ( [0] => 8******* [ID] => 8******* [1] => M***** [LAST_NAME] => M***** [2] => A***** [FIRST_NAME] => A***** [3] => M [MIDDLE_NAME] => M [4] => IT [DEPARTMENT] => IT [5] => HERE [LOCATION] => HERE ) 
I don't know if this is a good idea? I mean, I can just step 2 when I iterate to output or whatever; but, I gotta think there's a smoother way with mysql_fetch_assoc or mysql_fetch_row. But, I'm not 100% on working with these yet, they seem to just grab a single row? So, some other iterations would have to happen to get all the results?

Hmm.. I still get numerical and associative indexing...

Code: Select all

while($rowSupervisee = mysql_fetch_assoc($supervisee)){
     for($i=0; $i < mysql_num_rows($supervisee); $i++) {
          foreach ($rowSupervisee as $temp) {
               echo $temp;
          }
          echo "<br />";
     }
} 
- I totally meant to put this in the databases sub-forum, if a mod could move it I would appreciate it, sorry.

Re: Newbie: 'Correct' way of retreiving MySQL data

Posted: Thu Jun 04, 2009 3:36 pm
by Christopher
Moved to PHP Code as this is not a finished piece of code that you want other to review -- it is a PHP coding question.

Re: Newbie: 'Correct' way of retreiving MySQL data

Posted: Thu Jun 04, 2009 3:37 pm
by mikemike
I've never had to use numerical indexing from a MySQL result. I'm sure there are loads of uses for it, but I've never come across one. Because of this I've only ever used mysql_fetch_assoc.

However, I've been using CodeIgniter for ages now, even for smaller projects, because I find it so much quicker to roll things out now that I know all the function names. Because of this I just use CI's db class.