简体   繁体   English

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

[英]PHP/MySQL: How to get multiple values from a PHP database method

Sorry for the incredibly newbie question, but I can see myself drifting into bad practices if I don't ask. 抱歉,这个令人难以置信的新手问题,但是如果我不问的话,我会发现自己陷入了不良做法。

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

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

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: as Hammerstein points out you could use objects but its as good/bad as arrays depending on context. 编辑:正如Hammerstein指出的那样,您可以使用对象,但取决于上下文,其效果与数组一样好/坏。 Very similar. 非常相似。

You could use an object as your return type. 您可以使用对象作为返回类型。 So; 所以;

class MyReturnValue 
{
  public $Value1;
  public $Value2;
}

function getMyValues() 
{
  $results = GetDatabaseValues( ); // Assume this returns an associative array

  $result = new MyReturnValue();
  $result.Value1 = $results["Value1"];
  $result.Value2 = $results["Value2"];
}

Then in your code, you can refer to $result.Value1 etc. 然后在代码中,可以引用$ result.Value1等。

There is no PHP function to do this, so you will have to form an array from the results. 没有PHP函数可以执行此操作,因此您必须从结果中形成一个数组。

$column = array()
$query = mysql_query("SELECT * FROM table ORDER BY id ASC");
while($row = mysql_fetch_array($query)){
    $column[] = $row[$key]
}

Then pass $column to your view(HTML) 然后将$ column传递给您的视图(HTML)

foreach($column as $value)
{
   echo "<li>" . $value . "</li>";
}

You can have arrays in arrays, so if you have a table with several columns you could assign them to an array as separate arrays: 您可以在数组中包含数组,因此,如果您的表中有几列,则可以将它们作为单独的数组分配给数组:

$all_results = array();
foreach($rowInDatabase as $key => $value){
    // each row will be an array with a key of column name and value of column content depending how you get the data from the DB.
    $colname = $key;
    $colVal = $value; //this is an array
    $all_results[$colname] = $colVal; //append the current row to the array
    }

}

code like this will populate your array with an array per row of the table so if there are ten rows and five columns you could get row 2 column 3 with $all_results[1][2]; 像这样的代码将用表的每行数组填充您的数组,因此,如果有十行五列,则可以使用$ all_results [1] [2]来获得第2行第3列; (as they start from 0). (因为它们从0开始)。

Not quite sure I understand what you want to do fully, but you could always pass the result back from the method, and then loop through it in your HTML. 不太确定我是否完全理解您想要做什么,但是您始终可以将结果从方法传回,然后在HTML中循环遍历。

Your method would be something like: 您的方法将类似于:

public function my_method()
{
     $result = $db->query($sql_here);
     return $result;
}

And then your HTML would be 然后您的HTML将是

<select>

<?

    $result = $class->my_method();

    while($row = $result->fetch_assoc())
    {
        echo '<option>'.$row['some_col'].'</option>';
    }

?>

</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM