简体   繁体   English

PHP MySQLi使用foreach获取行

[英]PHP MySQLi get the row using foreach

I have this function that i created for getting the rows of data from mysqli database. 我具有为从mysqli数据库获取数据行而创建的此功能。

I try to used foreach but the only i got was something like i attached the image please view. 我尝试使用foreach,但唯一得到的是类似我附加的图片的图片,请查看。

oops sorry i can't post image it required me 10 reputation to do that. 抱歉,我无法发布图片,因此我需要10点信誉才能做到。

below : Yes these is what i got 1 2 x MM n OOX 下面:是的,这就是我得到的1 2 x MM n OOX

my codes below with form and php 我的以下代码与形式和PHP

## FOR SHORTHAND DATABASE
if ( !function_exists( 'hs_debugSQL' ) ) {
    function hs_debugSQL($hs_db, $string, $debug=0) {
        if ($debug == 1)
            print $string;
        if ($debug == 2)
            error_log($string);
        $result = mysqli_query($hs_db, $string);
        if ($result == false) {
            error_log("SQL error: ".mysqli_error($hs_db)."\n\nOriginal query: $string\n");
            // Remove following line from production servers 
            die("SQL error: ".mysqli_error($hs_db)."\b<br>\n<br>Original query: $string \n<br>\n<br>");
        }
        return $result;
    }
}

## FOR SELECT SQL
if ( !function_exists( 'hs_getrows' ) ) {
    function hs_getrows($sql, $debug=0) {
        $result = hs_debugSQL($sql, $debug);
        if($lst = mysqli_fetch_array($result)) { 
            mysqli_free_result($result); 
            return $lst; 
        }
        mysqli_free_result($result);
        return false;
    }
}

form 形成

<label for="message_title">Send to Admin <span class="red">*</span></label>
<input id="name" name="user_to" class="text" />
<select name="user_to">
    <?php
    global $hs_db;
    $get_list_admin = hs_getrows( $hs_db, "SELECT * FROM hs_users WHERE user_control = 'admin' " );
    foreach ( $get_list_admin as $keys ) {
        echo '<option value="'.$keys['user_login'].'">'.$keys['user_login'].'</option>';    
    }
    ?>
</select>

connection from mysqli is fine as you can see i can get the data from mysqli. 从mysqli的连接很好,因为您可以看到我可以从mysqli获取数据。 but when i used foreach to retrieve the data it only show 1 number and sometimes 1 character. 但是当我使用foreach检索数据时,它仅显示1个数字,有时还显示1个字符。

i like to learn what is wrong with my codes for foreach. 我喜欢学习foreach代码的问题。

thank you. 谢谢。

EXPLANATION 说明

The problem is here: 问题在这里:

if($lst = mysqli_fetch_array($result)) { 
     mysqli_free_result($result); 
     return $lst; 
}

mysqli_fetch_array returns row by row, you should loop through the result and returning the array. mysqli_fetch_array返回,您应该遍历结果并返回数组。 Something like this 像这样

$lst = array();
while ($lst = mysqli_fetch_array($result)){
    $lst[] = $row;
}
mysqli_free_result($result);

return $lst;

EDITED: 编辑:

Well, i see another extrange thing 好吧,我看到了另外一个东西

hs_debugSQL($sql, $debug);

and in the form 并以形式

hs_getrows( $hs_db,....)

Your are calling the function hs_debugSQL with the SQL you want to execute i guess but in your function the first param is the database link, and the second the SQL you want to execute. 我想您正在用要执行的SQL调用函数hs_debugSQL ,但是在您的函数中,第一个参数是数据库链接,第二个参数是您想要执行的SQL。 You should change this to match the params for the function. 您应该更改它以匹配该函数的参数。

And you are calling the function hs_getrows with the database link in the first param and in function declaration you expect the $sql so you should change this. 并且您在第一个参数和函数声明中通过数据库链接调用函数hs_getrows ,您期望使用$ sql,因此应对此进行更改。

I can't help but think that these 'helper' functions are making things more complicated than they should be. 我禁不住认为这些“辅助”功能使事情变得比应有的复杂。 There's more than one problem here, but here's a crucial one: 这里有一个以上的问题,但是这是一个关键的问题:

This function: 该功能:

    function hs_debugSQL($hs_db, $string, $debug=0) {

requires $hs_db, a dtabase connection resource, be passed in, while this code: 需要传入$ hs_db(一种dtabase连接资源),而此代码为:

function hs_getrows($sql, $debug=0) {
    $result = hs_debugSQL($sql, $debug);        // where is $hs_db?
    if($lst = mysqli_fetch_array($result)) { 
        mysqli_free_result($result); 
        return $lst; 
    }
    mysqli_free_result($result);
    return false;

fails to provide a that key argument. 无法提供那个关键论点。 Of course, further up the sequence of calls we find that $hs_db has been submitted, but has now been called $sql and the query seems to be missing. 当然,进一步上涨的调用序列,我们发现$hs_db 已经提交,但现在已经被称为$sql查询似乎缺少。

Simplify! 简化! Get rid of these unhelpful functions, clarify what you are trying to do and make something work. 摆脱这些无用的功能,弄清您要做什么并使其正常工作。 Then if you feel the need to encapsulate some of this again, go ahead and do so knowing that the code basically works. 然后,如果您觉得有必要再次封装其中的某些内容,请继续工作,并知道代码基本可以正常工作。

If you want to use a real helper 如果您想使用真正的助手

<?php
$sql = "SELECT user_login FROM hs_users WHERE user_control = 'admin'";
$admin_list = $hs_db->getCol($sql);
?>
<label for="message_title">Send to Admin <span class="red">*</span></label>
<input id="name" name="user_to" class="text" />
<select name="user_to">
<?php foreach ($admin_list as $admin): ?>
    <option value="<?=$admin?>"><?=$admin?></option>
<?php endforeach ?>
</select>

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

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