0

I have a form where I have individual textboxes on-screen where I want to insert a pupil name into each one. Each textbox will contain one pupil name. I have an SQL query that gets all the pupils I want for the class this form is created for. When the SQL is run in PHPMyAdmin, the query returns the exact values I want.

The code below is what I had hoped would be to get the data from the database and put it into an array so that I could put each row into a textbox later. However when I run this code, the finished array only contains 1 element and is the second row found by the query. The records found by query are;

+--------------------------+
| Pupil_Name (Column name) |
+--------------------------+
| Sam Price                |
| Keith mcgareth           |
+--------------------------+

Only Keith mcrgareth is echoed at the end. When both records should be echoed.

$pupilarray = array();
$pupilquery = mysqli_query($connection, "SELECT Pupil_Name FROM tbllogin WHERE Class_Name = 'Heulog'         AND Access_Level = 'pupil'");
while($row = mysqli_fetch_assoc($pupilquery)){
$pupilarray = $row['Pupil_Name'];
}
echo $pupilarray;

Have I created my array wrong? or am I missing something?

4 Answers 4

1

Since you put the echo right after the loop, you'll only get the last value. Put the echo inside if you want it to be printed as the loop goes on:

while($row = mysqli_fetch_assoc($pupilquery)){
    $pupilarray = $row['Pupil_Name'];
    echo $pupilarray . '<br/>';
}

But if your intent is to create an array, then you should push it with the array notation on the assignment:

$pupilarray = array(); // initialize an array named `pupilarray`
while($row = mysqli_fetch_assoc($pupilquery)){
    $pupilarray[] = $row['Pupil_Name'];
           //  ^ push the value inside
}

Sidenote: And if your thinking you could just straight up echo the array (echo $pupilarray). No you cannot do that. Either use print_r() or var_dump() to see its contents.

Sign up to request clarification or add additional context in comments.

Comments

0

You overwrite the $pupilarray variable, you don't add something to the array. Adding something to the array can be done with $pupilarray[] = ...

Comments

0

Try this..

$pupilarray = array();
$pupilquery = mysqli_query($connection, "SELECT Pupil_Name FROM tbllogin WHERE Class_Name = 'Heulog'         AND Access_Level = 'pupil'");
while($row = mysqli_fetch_assoc($pupilquery)){
$pupilarray[] = $row['Pupil_Name'];
}
print_r($pupilarray);

Comments

0

in you code you get last value, change it follow

mysqli_fetch_assoc ( mysqli_result $result ). Returns an associative array that corresponds to the fetched row

while($row = mysqli_fetch_assoc($pupilquery)){
$pupilarray[] = $row['Pupil_Name']; // value of Pupil_Name column put in array
$pupilarrayFull[] = $row; // Put each $row array in pupilarrayFull[] 
}

print_r($pupilarray) // your array is here of Pupil_Name column

print_r($pupilarrayFull) // your array is here of All column

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.