Get values from array from MySQL select

I have the following code:

$query = "SELECT r.ResID, r.ResFirstName, AptFloor
FROM residents r JOIN apartments a ON r.AptID = a.AptID
WHERE ResFirstName = 'Richard'
AND AptFloor = 2
";

$result = mysqli_query($con, $query) or die('No residents fit your criteria');

while($row=mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$data[] = $row;

echo "ID: $row[ResID]<br>";
echo "Name: $row[ResFirstName]<br>";
echo "Floor: $row[AptFloor]<br>";
echo " NOW FROM ARRAY:<br>";
echo "#0 $data[0]<br>";
echo "#1 $data[1]<br>";
echo "#2 $data[2]<br>";
}
echo "<br><br>";
var_dump($data);

I want to be able to use the variables in other statements. I expected the “$data[0]”, etc. to hold those variables, but the output I get is as follows:
ID: 162
Name: Richard
Floor: 2
NOW FROM ARRAY:
#0 Array
#1
#2
ID: 426
Name: Richard
Floor: 2
NOW FROM ARRAY:This text will be hidden
#0 Array
#1 Array
#2

array(2) { [0]=> array(3) { [“ResID”]=> string(3) “162” [“ResFirstName”]=> string(7) “Richard” [“AptFloor”]=> string(1) “2” } [1]=> array(3) { [“ResID”]=> string(3) “426” [“ResFirstName”]=> string(7) “Richard” [“AptFloor”]=> string(1) “2” } }

How do I get the variables only - not the other things in the dump?

Your code is doing exactly what it was written to do. After the end of the loop, $data[0] is an associative array consisting of the first row of fetched data, and $data[1] is an associative array consisting of the second row of fetched data.

What variables? An array is a variable, with more than one dimension?

After getting all the values from the SELECT query into the array, $data, I’d like to be able to use each value (e.g. “Richard”) in a table that I create after generating the array.

You would loop over the $data array and reference the desired associative index element(s) in each row -

foreach($data as $row)
{
    echo $row['ResFirstName'];
}

Thanks for your help! Is there a way to address one specific value, for example the second or third?

How do you know which specific row you want? You would write a query that matches that specific row of data, then just directly fetch the one row of data from the query (no loop required), and reference the desired associative index element(s) in that one row of data.

It’s a bit complicated. I querying to find residents with disabilities, listing them on a single page formatted by their apartment floors (this is for firemen to use for emergency evacuations). I was hoping to do a query for everyone with disabilities on one floor, then create a table with columns for the assigned emergency staircase, and list the residents in the appropriate row and column. I thought by querying every resident and then being able to list them by using something like $data(floor, staircase), I’d be able to then list them in the proper row/column. I guess I may have to do multiple queries to accomplish this.

You would use one query to get the data that you want, in the order that you want it. Since you are going to operate on the data by floor number, you would index/pivot the data using the floor number as the main array index when you fetch the data. You would then loop over the indexed/pivoted data to produce the output, floor by floor.

It is not really possible to help you, or to write code/query(ies), based ever increasing snippets of information. You will end up continually wasting time on partial answers, since they don’t take into account all the extents and limits of the problem.

That’s ok - you’ve already helped me enough. I appreciate it - thanks.

Sorry for my ignorance - I wasn’t understanding arrays but I’ve got it now! Appreciate your patience.

Sponsor our Newsletter | Privacy Policy | Terms of Service