Ankündigung

Einklappen
Keine Ankündigung bisher.

Warum wird mir die Anzahl der Spalten nicht dargestellt.

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Warum wird mir die Anzahl der Spalten nicht dargestellt.

    Hallo!
    irgendwo habe ich einen (blöden) Fehler - ich erkenne ihn nicht.

    Das Ergebnis de Scripts unten ist:

    10:09:03
    Fields:
    rows:28

    also sehe ich 28 Tabellen, aber mysqli_field_count zeigt NULL. Das verstehe ich nicht.
    Also scheint die Verbindung zur Datenbank zu funktionieren, ebenso das Query.

    Ähnlich http://www.meinelt-online.de/it/php/...bfrageergebnis versuche ich, das Ergebnis eines Selects dann variabel darzustellen, und dazu brauche ich die Spalten-Anzahl und die Namen.

    Hier mein Code:


    PHP-Code:
    $con=mysqli_connect($host,$user,$pass,$database);
    // Check connection
    if (mysqli_connect_errno())
    {
    echo 
    "<br>Failed to connect to MySQL: " mysqli_connect_error();
    }
    $result=mysqli_query($con,"SHOW TABLES;");
    if (
    mysqli_connect_errno())
    {
    echo 
    "<br>Failed to query to MySQL: " mysqli_connect_error();
    }
    $fields mysqli_field_count($result);
    if (
    mysqli_connect_errno())
    {
    echo 
    "<br>Failed to query to MySQL: " mysqli_connect_error();
    }

    date_default_timezone_set("Europe/Berlin");
    $timestamp time();
    echo 
    date("H:i:s",$timestamp)." ";

    echo 
    "<br> Fields:" mysqli_field_count($result); //<=========== hier kommt "nichts"
    echo "<br> rows:"mysqli_num_rows($result); 

    Vielen Dank für alle Hilfe!

    Bruno

  • #2
    Für mysqli_field_count musst du $con verwenden und nicht $result!
    Und verwende in Zukunft bitte [ PHP ]-Tags(ohne Leerzeichen natürlich) um deinen Code!

    Kommentar


    • #3
      Und mysqli_connect_errno() hilft nicht bei Query-Fehlern.

      Kommentar


      • #4
        Du brauchst weder Spaltenanzahl noch Namen.

        Hier mal aus meinem Fundus rausgekramt

        PHP-Code:
        <?php
        $db 
        'test';    // Datenbank die vewendet wird

        $mysqli = new mysqli("localhost""root"""$db);

        if (
        $mysqli->connect_errno) {
            echo 
        "Failed to connect to MySQL: (" $mysqli->connect_errno ") " $mysqli->connect_error;
        }

        $mysqli->set_charset("utf8");


        $query "SHOW TABLES";

        if (
        $result $mysqli->query($query)) {
            
        $tables $result->fetch_all(MYSQLI_NUM);
        }


        if ( isset(
        $_GET['table']) ){

            if ( 
        preg_match('#[A-z_0-9]#'$_GET['table']) && strlen($_GET['table']) < 65 ){
                
        $tablename $mysqli->real_escape_string($_GET['table']);
            }else{exit;}

        }else{
            
        // Starte mit 1. Tabelle
            
        $tablename $tables[0][0];
        }


        function 
        makeHTMLtable($result)
        {
            
        /*
            * Creates a HTML table
            * @Param result a mysqli result object
            * @return rendered html code or false
            **/

            
        if ( $result->num_rows ){
                return 
        false;
            }

            
        $html "\t<thead>\n\t\t<tr>\n";

            
        $row $result->fetch_assoc();

            foreach (
        $row as $key => $value) {
                
        $html .= "\t\t\t<th>" $key "</th>\n";
            }

            
        $html .= "\t\t</tr>\n\t\t</thead>\n\t\t<tbody>\n\t";

            while(
        $row $result->fetch_assoc()){     
                
        $html .= "\t<tr>\n";
                foreach (
        $row as $value) {
                    
        $html .= "\t\t\t<td>" htmlspecialchars($value) . "</td>\n";
                }
                
        $html .= "\t\t</tr>\n\t";
            }
            
        $html .= "\t</tbody>";

            return 
        $html;
        }

        $query "SELECT * from {$tablename} Limit 10";
        $result $mysqli->query($query);

        $tablecontent makeHTMLtable($result);
        ?>

        <!DOCTYPE HTML>
        <html lang="de">  
        <head>  
            <meta charset="UTF-8">  
            <title>Tabellendaten
            </title>
            <style>
             table, th, td { border: 1px solid; }
             table { border-collapse: collapse; margin-top: 1.5em;}
             thead>tr, tr:nth-child(even) {
             background-color: rgb(204, 225, 238);
            }
            tr{ line-height: 1.33em;}
            td, th, caption {
                border: 1px solid grey;
                padding: 0 5px;
                text-align: left;
                margin: 0;
            }
            </style>
        </head>  
        <body>   
            <form>
                <label>Tabellen:</label>
                <select name="table">
                <?php foreach ($tables as $table ): ?>
                <option value="<?= $table[0?>"><?= $table[0?></option>
                <?php endforeach ?>
                </select>
                <input type="submit" value="submit" name="submit">
            </form>
            <table>  
            <?= $tablecontent ?>
            </table>
        </body>
        </html>

        Kommentar

        Lädt...
        X