Page 1 of 1

Populate an Array from MySQL

Posted: Sun Dec 19, 2004 9:58 am
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.

Posted: Sun Dec 19, 2004 10:04 am
by patrikG
divulge your code ;)

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

Posted: Sun Dec 19, 2004 1:18 pm
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]

Posted: Mon Dec 20, 2004 3:34 am
by patrikG
Did I mention to use

Code: Select all

or

Code: Select all

tags where appropriate?

Posted: Mon Dec 20, 2004 3:47 am
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...