我在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