我在PHP中有一个函数,我想从我的MYSQL数据库中导出一个数组,然后使用循环中的行与他们做一些事情。如何在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)) {
你能帮帮我吗?
您的查询不起作用。 – 2015-02-23 13:10:34
奇怪的是,查询的工作原理如果我在函数之外使用它。第一个循环也起作用,然后显示错误。此外,错误报告也不会显示任何错误。 – triplus 2015-02-23 13:16:44
可能重复[mysql \ _fetch \ _array()期望参数1是资源(或mysqli \ _result),布尔给定](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter -1-to-resource-or-mysqli-result-boole) – 2015-02-23 14:06:27