Hello! How to make so that in the combobox the list was filled with values from the table in the MySQL database?
1 answer
$db = new PDO($connectionString, $user, $pass); $res = $db->query(“SELECT id, name FROM table”); echo "<select name='var'>"; while ($obj = $res->fetch(PDO::FETCH_OBJ)) { // $obj - экземпляр объекта stdClass, имена столбцов - свойства объекта echo "<option value='{$obj->id}'>{$obj->name}</option>" } echo "</select>";
This is how you can make a dropdownlist. (combobox?)
- one@ Vitaly Kustov, I won’t be surprised if you now have to answer the question of what PDO is))) - Deonis
- @Deonis, for a long time it was not clear to me why in projects I inherited from someone I had to meet something like mysql_ * or other quinoa. For over a year I have been watching the forums, this resource and many more than. And EVERYWHERE specialists advise beginners (setting an example, so to speak) to use this (and not only this) antiquity. And they (beginners) think that they have understood and the end is over. Guard. One Popov did so much shit. And my answer does not teach anything good. - Vitaly Kustov
- oneMixed SQL, HTML. There is simply no time for an explanation that flies should be separate - cutlets separately. It remains only to hope that a person is smart enough not just to take and copy. I just pointed out the principle. Pure abstraction - Vitaly Kustov
|