繁体   English   中英

如何在PHP函数中使用Mysql数组的行?

[英]How to use rows of Mysql array in PHP function?

我有一个PHP函数,我想从MYSQL数据库中导出一个数组,然后在循环中使用行对它们进行处理。

    $DB_Server = "XXX.XXX.XXX.XXX";
    $DB_Username = "XXXX";
    $DB_Password = "XXXXX";
    $DB_Database = "XXXXX";


    $conn = new mysqli($DB_Server, $DB_Username, $DB_Password, $DB_Database);

    $query = "Select Name, Wert from test.DBPBX";

function show(array $options) {
    global $showresult, $master, $conn, $query;
    $result = mysqli_query($conn, $query);

        while ($row = mysqli_fetch_assoc($result)) {
        $cnlongname =  $row["Name"];
        $usercontent = $row["Wert"];
        $cn = $cnlongname;
        $options['config']->value = 1;
        $config = $options["config"]->value;
        $show = new SimpleXMLElement("<show/>");
        $user = $show->addChild("user");
        $user->addAttribute("cn", $cn);
        if ($config)
            $user->addAttribute("config", "true");

        print "cmd: " . htmlspecialchars($show->asXML()) . "\n";

        $showresult = $master->Admin($show->asXML());
        print "result: " . htmlspecialchars($showresult) . "\n";

        $mod = "text=".$usercontent;
        $modify = new SimpleXMLElement("$showresult");

        $user = $modify->user;
        $path = explode("/device/hw/", $mod);
        $srch = $user;
        $nsegments = count($path);
        $i = 1;
        foreach ($path as $p) {
            if ($i == $nsegments) {
                // last part, the modification
                list($attr, $value) = explode("=", $p);
                $srch[$attr] = $value;
            } else {
                $srch = $srch->$p;
            }
            $i++;
        }
        $modify = new SimpleXMLElement("<modify>" . $user->asXML() . "</modify>");
        print "cmd: " . htmlspecialchars($cmd = $modify->asXML()) . "\n";

        // do it
        $result = $master->Admin($cmd);
        print "result: " . htmlspecialchars($result);
        }
    }

对于$ cn,我想使用$ cnlongname(或$ row [“ Name”])。 对于$ mod,我想使用$ usercontent(或$ row [“ Wert”])。 但是,当我想使用它时,在第一个循环后出现错误:

警告:mysqli_fetch_assoc()期望参数1为mysqli_result,在第175行的/sample.php中给出的字符串

175行是:

while ($row = mysqli_fetch_assoc($result)) {

你能帮我么?

在while循环中,您覆盖了结果,因此丢失了mysql结果,无法再查询它。

// do it
$result = $master->Admin($cmd);

在此使用其他变量。 :)

暂无
暂无

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

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