Help with getting Data from Table

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
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Help with getting Data from Table

Post by nishmgopal »

Hi guys, I am new to this forum and hope I can find some help.

I am new to PHP and MySql, infact I am new to programming!

Basically, I am trying to pull data from my database using the code below, but only the first record gets pulled out. I am not sure if I am using arrays correctly either!

What would I put in the echo section to display all the data from all the records?

Can anyone help?

Thank you in advance

Code: Select all

query = "SELECT * FROM Persons";
   $result = mysql_query($query);
 
   while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
      $Name = $row['Name'];
      $Java = $row['Java'];
      $Leadership = $row['Leadership'];
      $Communication = $row['Communication'];
      $Teamwork = $row ['Team_Work'];
      $Problem_Solving = $row['Problem_Solving'];
      }
 
 
$get_java = mysql_query("SELECT Name, Java FROM Persons WHERE
AND Java>=(SELECT Java FROM Job_Spec WHERE Project_Name='Manager')");
 
 
$Java3 = mysql_fetch_array($get_java);
{
      
 
 
// Displays results
      
    echo "";
    
    }
 
 
?>
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Help with getting Data from Table

Post by ben.artiss »

Code: Select all

<?php
$query = "SELECT * FROM Persons";
$result = mysql_query($query);
 
// I prefer mysql_fetch_assoc (mainly because I don't know how to use mysql_fetch_array!)
 
while ($row = mysql_fetch_assoc($result)) {
    $Name = $row['Name'];
    $Java = $row['Java'];
    $Leadership = $row['Leadership'];
    $Communication = $row['Communication'];
    $Teamwork = $row ['Team_Work'];
    $Problem_Solving = $row['Problem_Solving'];
 
    // Is this the information you want to show?
    // e.g
    // echo $Name.' / '.$Java;
}
 
// It's good practice to clear the result once you're done.
mysql_free_result($result);
 
// If the query below is the info you want to show, what's that stuff up there doing (cause it's doing nothing at the moment!)? :)
// By the way it's OK to re-use the variable $query and $result because we cleared the last result.
 
$query = "SELECT Name, Java FROM Persons WHERE Java>=(SELECT Java FROM Job_Spec WHERE Project_Name='Manager')";
$result = mysql_query($query);
 
while ($row = mysql_fetch_assoc($result)) {
    $name = $row['Name'];
    $java = $row['Java'];
 
    echo "$name / $java <br />";
}
 
mysql_free_result($result);
?>
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: Help with getting Data from Table

Post by nishmgopal »

Thank you for your help. I managed to get the information out.
Post Reply