无法在 php 中使用存储过程填充选择


require_once "Constants.php";
require_once "database.php";
require_once "fun_class.php";
$fun_class = new fun_class();
$database = new database();
$myConnection = $database->connect_database(SERVER,USER,PASSWORD,DATABASE);

我在页面顶部有这个(标题区域 - 那是因为我有会话的事情正在进行(,然后我在应该进行填充的地方有第二个 php 打开和关闭选项卡:

<select>
    <?php
    $fun_class->fillDropDownCategory($myConnection);
    ?>          
</select>

在我的fun_class.php中,我有这个功能:

 function fillDropDownCategory($mysqli) {
        echo " 1 ";
        $display_block = "";
        //Call Stored Procedure 
        if ($result = $mysqli->query("call rb_selectCategory()")) {         
        echo " 2 ";
        //  If there are no contacts returned
            if ($result->num_rows < 1) {
            //no records
                echo " 3 ";
                $display_block .= "<p><em>Sorry, no records to select!</em>
                </p>";
            } else {   
                echo " 4 ";
            //  For each record returned populate the select list
                while ($recs = $result->fetch_object()) {
                    $id = $recs['category_id'];
                    $display_name = stripslashes($recs['name']);
                    $display_block .= "<option value='" . $id . "'>" . 
                    $display_name . "</option>";
                }
            }
            //free result
            $result->free();            
            //Get the last id entered - needed for the other tables
            echo " 5 ";     
            //So you can run another stored proedure after a select
            $mysqli->next_result();
        }
        else
        {
        echo " 6 ";
        $display_block .= "<p><em>Sorry, no records to select!</em></p>";
    }
        echo " 7 ";
        return($display_block);
    }

当我进入页面时,选择是空的,当我输入网站的源代码时,我可以看到:

           <select>
            1  6  7         
           </select>

这是我的调试回显的输出

我的存储过程是:

BEGIN
SELECT category_id, name AS name
FROM xxx.category
ORDER BY name;
END

当执行时(在 phpmyAdmin 中(,它返回两个表,一个称为 category_id 的 id,第二个称为 name,它具有分配给 id 的类别名称。

我对 php 很陌生,我的函数可能有问题,但由于缺乏经验,我找不到错误。

我有

$id = $recs['category_id'];
$display_name = stripslashes($recs['name']);

将其更改为

$id = $recs->category_id;
$display_name = stripslashes($recs->name);

感谢大家的尝试!

由于您需要在 HTML 中打印这些字符串,因此需要使用 echo 等函数。在您的fillDropDownCategory()函数中,您正在回显某些内容(例如 echo " 1 "; (,但最后你返回一个变量。

这个变量不会仅仅因为它被返回而被打印,所以你需要

通过
<select>
    <?php
    echo $fun_class->fillDropDownCategory($myConnection);
    ?>          
</select>

当然,您还需要删除fillDropDownCategory()中的echo调用,以避免在HTML中打印一些不需要的字符串。

最新更新