0

I have a function in PHP and I would like to export an array from my MYSQL Database and then use the rows in a loop to do some stuff with them.

    $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);
        }
    }

For $cn I would like to use $cnlongname (or $row["Name"]). And for $mod I would like to use $usercontent (or $row["Wert"]). However when I would like to use it I get an error after the first loop:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, string given in /sample.php on line 175

Line 175 is:

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

Can you please help me?

triplus
  • 91
  • 5
  • 17
  • your query doesnt work. – Mustafa Toker Feb 23 '15 at 13:10
  • The strange thing is, that the query works If I use it outside of the function. Also the first loop works, and after that the error display. Also error reporting doesn't show any errors. – triplus Feb 23 '15 at 13:16
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource (or mysqli\_result), boolean given](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole) – Jay Blanchard Feb 23 '15 at 14:06
  • After further testing, I might have problems with the While Clause. If I declare a local array and use it in a foreach loop in the function it works. Very strange. And I even don't get any error messages. I'm trying it with PDO now. Maybe this might help. – triplus Feb 23 '15 at 14:28

1 Answers1

1

Inside your while loop you overwrite your result, therefore you lose your mysql result and cannot query it any more.

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

Use a different variable there. :)

sanados
  • 104
  • 3
  • Thank you. I also tried it with PDO and this works fine as well and saves me some line of code ;) – triplus Feb 23 '15 at 15:50