Populate an Array from MySQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
dougp25
Forum Newbie
Posts: 10
Joined: Tue Mar 30, 2004 12:29 pm

Populate an Array from MySQL

Post by dougp25 »

Here's what I am trying (rather unsuccessfully!) to do:

I have a database, called "Students" which has these fields: "Lastname", "Firstname", "GradeLevel", and "Subject". In my php script, I do a query: "SELECT * FROM Students WHERE GradeLevel = 7"; and if a do a row count, it returns 88 rows (that's good!) I want to "walk thru the array", but show me the fields in the array. So, the first one might be "Adams Brian 7 Math" and the next one might be "Angeluz Teresa 7 Science" and etc. I need this because my plan is that once a teacher enters their grade, I would present them with a web page that would say "Enter Grades for " [first kid] and they would do so. By pressing "Next", it would write the data to another table, and then say "Enter Grades for " [second kid], etc. until done.

I have no trouble with the query, or INSERTING data into the 2nd table. The problem appears to be my array! Can someone help?

Thanks.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

divulge your code ;)
dougp25
Forum Newbie
Posts: 10
Joined: Tue Mar 30, 2004 12:29 pm

here's my code (it ain't pretty)

Post by dougp25 »

Lots of stuff commented out, doesn't do what I want it to do!
patrikG | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Code: Select all

<html><head><title>Enter Progress Report</title>
</head><body>
<?php
echo "<p>Welcome <b>$_POST[teacher]</b></p>";
echo "<p>You are ready to enter progress reports for $_POST[term] in Grade
$_POST[grade] for $_POST[subject]</b></p>";
//open connection to database
$conn = mysql_connect("localhost", "baroot", "25rtf6");
mysql_select_db("students",$conn);
//create the sql statement
$sql = "SELECT * FROM students WHERE GRADE = $_POST[grade]";
//now execute the sql
$result = mysql_query($sql, $conn) or die(mysql_error());

//get the # of rows in the result set
$number_of_rows = mysql_num_rows($result);
echo "The number of rows is $number_of_rows";
//
//
//VERY COOL
$kids = mysql_fetch_array($result);
$current_kid = current($kids);
        $kidfirst = $kids['FNAME'];

if ($current_kid)
        //print("$current_kid<BR>");
        echo $kidfirst;
else
        print("Nothing to print");

//      $kidlast = $kids['LNAME'];
//      $kidID = $kids['ID'];
//      $kidgrade = $kids['GRADE'];
//      $kidHR = $kids['HR'];
?>
</body></html>
patrikG | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Did I mention to use

Code: Select all

or

Code: Select all

tags where appropriate?
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Code: Select all

$sql = mysql_query("SELECT whatever FROM table");
while($row = mysql_fetch_assoc($sql)){
    $array[] = each($row);
}
Would that work? It's too late to try it...
Post Reply