【发布时间】:2015-02-23 13:06:14
【问题描述】:
我在 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,在 /sample.php 中给出的字符串 第 175 行
第 175 行是:
while ($row = mysqli_fetch_assoc($result)) {
你能帮帮我吗?
【问题讨论】:
-
您的查询不起作用。
-
奇怪的是,如果我在函数之外使用它,查询就可以工作。第一个循环也有效,然后显示错误。错误报告也不会显示任何错误。
-
经过进一步测试,我可能对 While 子句有问题。如果我声明一个本地数组并在函数的 foreach 循环中使用它,它就可以工作。很奇怪。而且我什至没有收到任何错误消息。我现在正在尝试使用 PDO。也许这可能会有所帮助。