Skip to content
Advertisement

Fetch data from MySQL using OOP

I’m a beginner in OOP PHP. I’m trying to make a class that will connect,query and fetch data I done the below coding

JavaScript

what I’m trying to achieve is this

JavaScript

and I want to fetch the data using a format like this

JavaScript

but I’m not getting that logic to make this happen please help

Thanks!

Advertisement

Answer

Your Fetch function is only pulling one row from the database, and you aren’t returning the results…

The method isn’t the best, but in order to achieve what you’re trying to do:

JavaScript

This will make each row a part of $result, but you’ll have to access it like this:

You would call the fetch function like this:

JavaScript

Or in a loop:

JavaScript

That said, fetching the entire result set into memory is not a great idea (some would say it’s a very bad idea.)

Edit: It also looks like you have other issues with your class… I have to run but will check back tomorrow and if others haven’t set you straight I will expand.

Edit2: Ok, going to do a once-over on your class and try to explain a few things:

JavaScript

I hope this helps… It should get you started at least. If you have more questions you should ask them separately.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement