How to access mysql result set data with a foreach loop

0 votes

I'm working on a PHP application that queries MySQL using a database class. This link will take you to the class: real-world oop using PHP and MySQL:  http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql/

The lesson should not be regarded as a modern reference because it illustrates numerous incorrect practices! I modified the class to better suit my needs, but there is a problem (maybe a stupid one). Using select(), a multidimensional array with three associative columns (id, first name, last name) each row is returned:

Array
(
    [0] => Array
        (
            [id] => 1
            [firstname] => Firstname one
            [lastname] => Lastname one
        )

    [1] => Array
        (
            [id] => 2
            [firstname] => Firstname two
            [lastname] => Lastname two
        )

    [2] => Array
        (
            [id] => 3
            [firstname] => Firstname three
            [lastname] => Lastname three
        )
)

Now I want to use MySQL fetch assoc() to use this array as a result. Although this is with simple/flat arrays, I am aware that it may be used with foreach(). I believe I must declare a new foreach() inside of each foreach(), however, I believe this may slow down or increase server load.

So, what is the simplest approach to use foreach() with this multidimensional array?

Jul 29 in PHP by Kithuzzz
• 3,660 points
30 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
0 answers

How to filter data from a MySQL Database Table with PHP

I'm attempting to code a search box ...READ MORE

Jul 22 in PHP by narikkadan
• 11,280 points
11 views
0 votes
1 answer

How to delete a certain row from mysql table with same column values?

Hello @kartik, Add a limit to the delete query delete from orders where ...READ MORE

answered Aug 26, 2020 in PHP by Niroj
• 82,740 points
629 views
0 votes
1 answer

How I can make a query with MySQL that checks if the valuein a certain column contains certain data?

Hello @kartik, Try this query: mysql_query(" SELECT * FROM `table` WHERE `column` ...READ MORE

answered Sep 14, 2020 in PHP by Niroj
• 82,740 points
751 views
0 votes
1 answer

How to use a string path to set nested array data?

Hello @kartik, Use the reference operator to get ...READ MORE

answered Nov 23, 2020 in PHP by Niroj
• 82,740 points
894 views
0 votes
0 answers

How to run a PHP function due to a button click (with button 'data' passed)?

I manage some sortiment items on a ...READ MORE

May 28 in PHP by Kichu
• 19,040 points
1,019 views
0 votes
0 answers

How to extract and access data from JSON with PHP?

This is intended to be a general ...READ MORE

Jul 22 in PHP by narikkadan
• 11,280 points
14 views
0 votes
1 answer

How to check if array is multidimensional or not?

Since the 'second dimension' could be just ...READ MORE

answered Nov 5, 2018 in Others by DataKing99
• 8,240 points
3,977 views
0 votes
1 answer

How to store input value into array then localstorage?

Hello @ abhittac, You have create the array everytime the ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,740 points
4,897 views
0 votes
0 answers

For-each over an array in JavaScript

How can I loop through all the ...READ MORE

Feb 8 in Java by Rahul
• 9,000 points
24 views
0 votes
1 answer

Convert a PHP object to an associative array

Start with simply typecasting the line:- $array = ...READ MORE

answered Feb 23 in PHP by Aditya
• 7,300 points
185 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP