I'm trying to get some code to populate a select field on a form with the contents of a field in a MySql database. I've got as far as the code below (with some help) BUT it only returns the first row in the table. Any ideas as to how I can get the full listing to display?
ps:
1. indentation *in most case* will help to understand the code more.
2. your mysql wrapper class isn't functional, you're reinventing the wheel. if you want to do so, maybe you can write the interface class first and then implement them using your mysql class.
3. this is just a suggestion from somebody unknown. you don't even have to listen to..
pps:
of course, i don't guarantee the code to work..
__________________
just an ignorant noob with moronic solution...
2. your mysql wrapper class isn't functional, you're reinventing the wheel. if you want to do so, maybe you can write the interface class first and then implement them using your mysql class.
Does this mean the mysql.class.php needs to be rewritten? I was given the code and this is starting to get way over my head!
Has anyone any other suggestions as to how I can get the data from a field in the database to populate a form select field?
uhm, maybe functional is not a proper word to call it. at a glance, your wrapper class should work because it's only redefining native php functions into class methods. it's like adding cosmetics to something which actually can stay in its best form without any add-ons. if you're making a wrapper class (in this case the mysql class), first you should think why you need it, whether it's only some syntactic sugar or it's really needed to satisfy your system concept and design (maybe the mvc).
moving from structural programming to object oriented programming sometimes brings confusion on dealing with the new aspect of oo programming. if one considers oo programming is just another way to list your functions and variables in a more fashioned way, he should never realize the power of oo programming and the reason why its development is rapidly increasing nowadays. from your mysql class, we can take this as example:
from the code above, it seems that all functions do the same thing with its original. if someone says, why must we rewrite them while they purely do the same with their ancestors? i won't answer the question here, because i'm not the code author . anyway, if we change above class above, maybe we can answer the question easily:
PHP Code:
class MySQL
{
var $sDBHost = '';
var $sDBName = '';
var $sDBUser = '';
var $sDBPass = '';
var $nPort = '';
var $hLink = '';
var $curRes = '';
var $aErrors = array();
//Usage example
$conn = MySQL::getInstance('localhost','mydb','myuser','mypass');
//your query
$sql = 'select foo from bar';
if(!$conn->ExecQuery($sql) && !$conn->NumRows()) {
//throw exception here
}
while($conn->FetchArray()) {
//print the options here
}
here, you have your singleton class so that it only has one instance (it's important to not burdening your mysql server). if you take a look at the class methods, you'll notice that some can accept empty param which if it happens, the method will call from the internal property. this will simplify your code a bit by not supplying parameters to each class function and the most important thing, your oo code shows its power.
just a note, php 4 oo is sometimes criticized by the oo purists. it's quite natural since php 4 is not designed to serve oo. if you know how to program oo in php, you must realize that there are not private and public functions and properties. php 4 polymorphism is also a bit tricky since you have to manipulate your extended class to behave as if it were implementing the interface.
common issue in oo programming is how to make oo code not bloated. don't worry, we have the answer here. php is share-nothing architecture. even bad codes can run fast in php environment (but please don't take this as excuse). by lots of exercise you'll then be able to optimize your oo code and then realize how helping the methodoloy is when you're working on big projects.
ps: as usual, i don't guarantee the code to work, and you don't even have to pay attention to this meaningless response .
__________________
just an ignorant noob with moronic solution...