PHP my Combobox will not populate from mysql

n Java, If you want to populate a combobox you first get into an array then populate it into the Combobox. I am assuming the process is similar in PHP. As you probably have guessed I am new to PHP. Loving it’s power just need to learn how to harness it.

I have an array using the fetchAll(); and storing it into $data. Then I run a for loop and storing into a new variable called $row. I am able to print my entire database out using the print_r method. Boom, my whole array is there. Then I think I stumbled onto away of putting into a combobox. The problem is it’s blank.

When I try to put the print_r method inside the loop and print the content I get the first place in my array. If I do this in Java, it should print everything as it goes through the loop. This is not happening. Then Depending on the location i put the print_r method it will just put the entire database into my combobox. The text that is inputted into the Combobox is an array format so it’s no good. Any suggestions or thoughts?

[php]
try{
$conn = new PDO(‘mysql:host=Jamal-PC;dbname=japanesewords’,$username,$password);
$sql =‘SELECT Englishword FROM Japanesedefinition;’;
$stmt = $conn->prepare($sql);
$stmt ->execute();
$data = $stmt->fetchAll(PDO::FETCH_COLUMN);

}catch(PDOException $e){
echo 'ERROR: ’ . $e->getMessage();
}

?>

<?php foreach($data as $row) : ?> <?= $row['EnglishWord']; ?> <?php endforeach ?>

[/php]

First of all, if this is one of your first attempts of PHP I’ve got to commend you. It shows a lot of promise and it clearly shows you have experience coding ^^

In case you or anybody else wonder what I think is awesome:

[ul][li]Using PDO (thank you)[/li]
[li]Clearly separating logic and view[/li]
[li]indention and short echoes (readability)[/li][/ul]

First of all, the query

[php]$sql =‘SELECT Englishword FROM Japanesedefinition;’;[/php]
Later on in the code it seems like you’re using both row id and row Englishword, in which case you have to fetch both.
[php]$sql =‘SELECT id, Englishword FROM Japanesedefinition’;[/php][size=8pt]I removed the semi colon from the query, just thought the double semi colons didn’t look good :stuck_out_tongue:[/size]
[hr]

[php]$data = $stmt->fetchAll(PDO::FETCH_COLUMN)[/php]
Afaik “fetch_column” will fetch a single column only. Later on in your code you’re clearly using an array with multiple values, so this would have to be changed to

[php]$data = $stmt->fetchAll(PDO::FETCH_ASSOC)[/php]
[hr]

But! I wouldn’t use an array, PHP is trying to be object oriented these days (PDO is), so why not fetch objects? Later on you could even fetch classes which will take each selected row from the DB and initialize a class of your choice. Great for users, items, articles, etc.

[php]try{
$conn = new PDO(‘mysql:host=Jamal-PC;dbname=japanesewords’,$username,$password);
$sql =‘SELECT id, Englishword FROM Japanesedefinition’;
$stmt = $conn->prepare($sql);
$stmt ->execute();
$data = $stmt->fetchAll(PDO::FETCH_OBJ);

}catch(PDOException $e){
echo 'ERROR: ’ . $e->getMessage();
}

?>

<?php foreach($data as $row) : ?> <?= $row->EnglishWord; ?> <?php endforeach ?> [/php]

Thank you so much the code worked correctly. Thank you for clearly pointing out my mistakes and where I went wrong. It was indeed a learning experience.

Can you recommend any tutorials on learning PHP? Also, now I am able to populate my combobox the next step is to have javascript populate the answers in my database. I haven’t learned Javascript yet, but I think I can pick it up quickly. Any suggestions on where I can learn some of these tools.

Thanks.

As for PHP there are almost too many resources out there, unfortunatly many are written by people who shouldn’t have been writing tutorials as much as reading them.

It all depends on your learning style really, I’ve never been too fond of following a book cover to cover. I’m much more prone to “learn as I go”.

php.net should be in your bookmarks bar as it is an awesome resource. Also just googling whatever you want to do or whichever error message you get should give you thousands of hits with solutions.

JimL, I respectfully disagree with the short tag echoes. It does make it a little easier to read, but this is an option that must be on in the .ini file. In the PHP 5.3.0 production file it is disabled by default. If you don’t have access to this on your web server you will have to go back and redo every opening php tag.

To me the risk is not worth the reward (or laziness).

It all depends on the developer. 5.3 is 2 years old now (?), I would recommend finding a new host :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service