MySQLi

Her kan du få hjælp og stille spørgsmål til PHP!
Besvar
MadsHaupt
Entusiast
Entusiast
Indlæg: 48
Tilmeldt: 8. jan 2013, 14:00

MySQLi

Indlæg af MadsHaupt » 7. jan 2014, 18:43

Kan man have et prepare statement inde i et prepare statement?.

MadsHaupt
Entusiast
Entusiast
Indlæg: 48
Tilmeldt: 8. jan 2013, 14:00

Re: MySQLi

Indlæg af MadsHaupt » 7. jan 2014, 18:46

Det prepare statement jeg har inde i et andet prepare statement, udtrækker ikke noget data, havd kan der være galt?.

Kode:

Kode: Vælg alt

<?php

session_start();

?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <style>
            label {
                margin-left: 20px;
            }
        </style>
    </head>
    <body>
        <?php
            
            $mysqli = new mysqli("madshaupt.dk.mysql", "madshaupt_dk", "rmJ5Kyr8", "madshaupt_dk");
            
            $programdir = opendir(".");
            while ($file = readdir($programdir)) {
                if (filetype($_SERVER["DOCUMENT_ROOT"]."/Pro/$file") == "dir" && $file != "." && $file != "..") {
                    if (file_exists($_SERVER['DOCUMENT_ROOT']."/Pro/".$file."/menuHTM.txt")) {
                        $fileopen = fopen($_SERVER['DOCUMENT_ROOT']."/Pro/".$file."/menuHTM.txt", "r");
                        $name = explode("Name: ", fgets($fileopen));
                        fclose($fileopen);
                        $name = $name[1];
                        $all;
                        echo "<label>$name</label><br />";
                        $program = $file;
                        $dir = opendir($program."/Installers");
                        while ($file2 = readdir($dir)) {
                            $ex = explode(".", $file2);
                            $size = sizeof($ex) - 1;
                            $ex = $ex[$size];
                            if (filetype($_SERVER['DOCUMENT_ROOT']."/Pro/".$program."/Installers/".$file2) == "file" && $ex == "exe") {
                                //$files[] = substr($file2, strlen($name) + 1, strlen($file2) - 5 - 10 - strlen($name));
                                $version = substr($file2, strlen($name) - 1, strlen($file2) - 13 - strlen($name));
                                $value;
                                $itemname = $file."_".$version;
                                if ($stmt = $mysqli->prepare("SELECT `value` FROM `ProStatus` WHERE `name` = ?")) {
                                    $stmt->bind_param("s", $itemname);
                                    $stmt->execute();
                                    $stmt->bind_result($value);
                                    $stmt->fetch();
                                    $stmt->close();
                                }
                                $all += $value;
                                echo "<label>".$version.": ".$value."</label><br /><label>Detaljer</label><br />";
                                $usercount;
                                $uname;
                                if ($stmt = $mysqli->prepare("SELECT * FROM `ProStatus`")) {
                                    $stmt->execute();
                                    $stmt->bind_result($uname, $usercount);
                                    while ($stmt->fetch()) {
                                        if (substr($uname, strlen($uname) - strlen($itemname) - 1) == "_".$itemname && strlen($uname) > $itemname) {
                                            $username = substr($uname, 0, strlen($uname) - strlen($itemname) - 1);
                                            $fornavn;
                                            $efternavn;
                                            if ($stmt2 = $mysqli->prepare("SELECT `fornavn`, `efternavn` FROM `users` WHERE `brugernavn` = ?")) {
                                                $stmt2->bind_param("s", $username);
                                                $stmt2->execute();
                                                $stmt2->bind_result($fornavn, $efternavn);
                                                $stmt2->fetch();
                                                $stmt2->close();
                                            }
                                            echo "$username";
                                            echo "<label>$fornavn $efternavn: $usercount</label><br />";   
                                        }
                                    }
                                    $stmt->close();
                                }
                                echo "<br />";
                            }
                        }
                        closedir($dir);
                        echo "<label>I alt: $all</label><br /><br /><br />";
                    }
                }
            }
            closedir($programdir);
        ?>
    </body>
</html>

MadsHaupt
Entusiast
Entusiast
Indlæg: 48
Tilmeldt: 8. jan 2013, 14:00

Re: MySQLi

Indlæg af MadsHaupt » 7. jan 2014, 18:52

Har får også en mysql error.

Fejlmeddelse: Commands out of sync; you can't run this command now

MadsHaupt
Entusiast
Entusiast
Indlæg: 48
Tilmeldt: 8. jan 2013, 14:00

Re: MySQLi

Indlæg af MadsHaupt » 7. jan 2014, 19:06

Nu, har jeg læst problemet man kunne altså ikke lave et prepare statement inde i et prepare statement.

Løsning: Man skulle bare registre data i en array, og når mysql kaldet er færdigt så skal man lave et for loop.

Kode:

Kode: Vælg alt

<?php

session_start();

?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <style>
            label {
                margin-left: 20px;
            }
        </style>
    </head>
    <body>
        <?php
            
            $mysqli = new mysqli("madshaupt.dk.mysql", "madshaupt_dk", "rmJ5Kyr8", "madshaupt_dk");
            
            $programdir = opendir(".");
            while ($file = readdir($programdir)) {
                if (filetype($_SERVER["DOCUMENT_ROOT"]."/Pro/$file") == "dir" && $file != "." && $file != "..") {
                    if (file_exists($_SERVER['DOCUMENT_ROOT']."/Pro/".$file."/menuHTM.txt")) {
                        $fileopen = fopen($_SERVER['DOCUMENT_ROOT']."/Pro/".$file."/menuHTM.txt", "r");
                        $name = explode("Name: ", fgets($fileopen));
                        fclose($fileopen);
                        $name = $name[1];
                        $all;
                        echo "<label>$name</label><br />";
                        $program = $file;
                        $dir = opendir($program."/Installers");
                        while ($file2 = readdir($dir)) {
                            $ex = explode(".", $file2);
                            $size = sizeof($ex) - 1;
                            $ex = $ex[$size];
                            if (filetype($_SERVER['DOCUMENT_ROOT']."/Pro/".$program."/Installers/".$file2) == "file" && $ex == "exe") {
                                //$files[] = substr($file2, strlen($name) + 1, strlen($file2) - 5 - 10 - strlen($name));
                                $version = substr($file2, strlen($name) - 1, strlen($file2) - 13 - strlen($name));
                                $value;
                                $itemname = $file."_".$version;
                                if ($stmt = $mysqli->prepare("SELECT `value` FROM `ProStatus` WHERE `name` = ?")) {
                                    $stmt->bind_param("s", $itemname);
                                    $stmt->execute();
                                    $stmt->bind_result($value);
                                    $stmt->fetch();
                                    $stmt->close();
                                }
                                $all += $value;
                                echo "<label>".$version.": ".$value."</label><br /><label>Detaljer</label><br />";
                                $usercount;
                                $uname;
                                $usercounts;
                                $unames;
                                if ($stmt = $mysqli->prepare("SELECT * FROM `ProStatus`")) {
                                    $stmt->execute();
                                    $stmt->bind_result($uname, $usercount);
                                    while ($stmt->fetch()) {
                                        if (substr($uname, strlen($uname) - strlen($itemname) - 1) == "_".$itemname && strlen($uname) > $itemname) {
                                             $usercounts[] = $usercount;
                                             $unames[] = $uname;
                                        }
                                    }
                                    $stmt->close();
                                }
                                for ($i = 0; $i < sizeof($usercounts); $i++) {
                                    $usercount = $usercounts[$i];
                                    $uname = $unames[$i];
                                    $username = substr($uname, 0, strlen($uname) - strlen($itemname) - 1);
                                    $fornavn;
                                    $efternavn;
                                    if ($stmt2 = $mysqli->prepare("SELECT `fornavn`, `efternavn` FROM `Users` WHERE `brugernavn` = ?")) {
                                        $stmt2->bind_param("s", $username);
                                        $stmt2->execute();
                                        $stmt2->bind_result($fornavn, $efternavn);
                                        $stmt2->fetch();
                                        $stmt2->close();
                                    }
                                    echo "<label>$fornavn $efternavn: $usercount</label><br />";  
                                }
                                echo "<br />";
                            }
                        }
                        closedir($dir);
                        echo "<label>I alt: $all</label><br /><br /><br />";
                    }
                }
            }
            closedir($programdir);
        ?>
    </body>
</html>

Besvar