return mysql_fetch_assoc results as an array

huangapple go评论83阅读模式
英文:

return mysql_fetch_assoc results as an array

问题

我试图从MySQL查询中检索结果并将其作为数组返回。以下是代码示例:

function get_view_options($table) {
    include('config.php');
    $conn = new mysqli($sql['host'],$sql['username'],$sql['password'],$sql['database']);
    if ($conn->connect_error){
        die("Connection failed: " . $conn->connect_error);
    }
    $query = "SELECT name FROM options WHERE page='$table' AND type='view' AND value='1'";
    $result = $conn->query($query);
    $options = array();
    while($row = $result->fetch_assoc()) {
        $options[] = $row['name'];
    }
    $conn->close();
    return $options;
}
$options = get_view_options($page);

这里是我希望你能获得的结果:

Array ( [0] => location [1] => owner [2] => id )

如何实现这个目标的任何建议都将不胜感激。

英文:

I am trying to retreive results from a mysql query as an array. Here is a sample of the code :

function get_view_options($table) {
    include('config.php');
    $conn = new mysqli($sql['host'],$sql['username'],$sql['password'],$sql['database']);
    if ($conn->connect_error){
        die("Connection failed: " . $conn->connect_error);
    }
    $query = "SELECT name FROM options WHERE page='".$table."' AND type='view' AND value='1'";
    $options = $conn->query($query)->fetch_assoc();
    $conn->close();
    return $options;
}
$options = get_view_options($page);

Here is the result I get from print_r ($options); :
Array ( [name] => location )

Here is what I was hoping to get :
Array ( [0] => location [1] => owner [2] => id )

Any suggestion on how I can achieve this would be much appreciated.

答案1

得分: 0

感谢 @aynber!以下是使用 fetch_all() 的最终代码:

<?php
    function get_view_options($table) {
        include('config.php');
        $conn = new mysqli($sql['host'],$sql['username'],$sql['password'],$sql['database']);
        if ($conn->connect_error){
            die("Connection failed: " . $conn->connect_error);
        }
        $query = "SELECT name FROM options WHERE page='".$table."' AND type='view' AND value='1'";
        $options = $conn->query($query)->fetch_all();//changed from fetch_assoc()

        $conn->close();
        return $options;
    }
    //We create an array with all the results
    $options = array();
    foreach(get_view_options($page) as $option){
        array_push($options,$option[0]);
    }
    print_r ($options);
?>

输出为:Array ( [0] => location [1] => owner [2] => id )

英文:

Thanks @aynber!
Here is the final code with fetch_all()

&lt;?php
    function get_view_options($table) {
        include(&#39;config.php&#39;);
        $conn = new mysqli($sql[&#39;host&#39;],$sql[&#39;username&#39;],$sql[&#39;password&#39;],$sql[&#39;database&#39;]);
        if ($conn-&gt;connect_error){
            die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);
        }
        $query = &quot;SELECT name FROM options WHERE page=&#39;&quot;.$table.&quot;&#39; AND type=&#39;view&#39; AND value=&#39;1&#39;&quot;;
        $options = $conn-&gt;query($query)-&gt;fetch_all();//changed from fetch_assoc()

        $conn-&gt;close();
        return $options;
    }
    //We create an array with all the results
    $options = array();
    foreach(get_view_options($page) as $option){
        array_push($options,$option[0]);
    }
    print_r ($options);
?&gt;

Which outputs : Array ( [0] => location [1] => owner [2] => id )

huangapple
  • 本文由 发表于 2020年1月4日 00:26:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定