Newbie: 'Correct' way of retreiving MySQL data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmX
Forum Newbie
Posts: 10
Joined: Fri Apr 03, 2009 2:13 pm

Newbie: 'Correct' way of retreiving MySQL data

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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.
(#10850)
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

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

Post 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.
Post Reply