PHP/MySQL:如何从PHP数据库方法获取多个值

问题描述:

对这个令人难以置信的新手问题深表歉意,但是如果我不问我,我会发现自己陷入了不良习惯之中.

Sorry for the incredibly newbie question, but I can see myself drifting into bad practices if I don't ask.

我有一个PHP方法,我想返回给定数据库列的所有值,以便将内容放在HTML表单的下拉菜单中.我显然可以在PHP方法中构造整个HTML并将其作为字符串返回,但是我认为这是非常糟糕的做法.

I have a PHP method that I want to return all the values of a given database column, in order to place the contents in a dropdown menu for a HTML form. I could obviously construct the whole HTML in the PHP method and return that as a string, but I imagine this is pretty bad practice.

由于PHP方法只能返回一个值,我想我需要多次调用该方法以填充下拉菜单,或从该方法传递一个数组.

Since PHP methods can only return one value, I imagine I'll need to call the method several times to populate the dropdown menu, or pass an array from the method.

什么是解决这个(大概)常见问题的好方法?谢谢.

What would be a good solution to this (presumably) common problem? Thanks.

好吧,数组是一个值,包含大量其他值.因此,只需让您的方法返回结果数组即可.

Well, an array is one value, containing tons of other values. So just have your method return a array of results.

edit:正如Hammerstein指出的那样,您可以使用对象,但取决于上下文,其效果与数组一样好/坏.非常相似.

edit: as Hammerstein points out you could use objects but its as good/bad as arrays depending on context. Very similar.