I few weeks ago, I posted some code that I need help converting to MySQLi, now I need some help converting it to PDO. The code allows you to enter queries and commands and brings back the column headings and the information that I am looking for into a table. I did try out a few combination of things before posting it here. I am almost there, but my column headings are off by a few. Below is the original code and any help is greatly appreciated. Thank you.

                if($type =="query") {

                    $qtext2 = str_replace("\\", " ", $qtext);

                        if($result = DB::inst()->query("$qtext2"))
                            _e( _t( "Successly Executed - " ) );
                        else
                            echo "<font color=red>Not able to execute the query<br>Either the 
                                table doesnot exist or a wrong query.</font><br><br>";

                        _e( _t( "Query is : " ) );
                        echo("<font color=blue>".$qtext2."</font>\n");

                        echo "<table class=\"dynamicTable tableTools table table-striped table-bordered table-condensed table-white\">
                        <thead>
                        <tr>\n";

                        $sds = $result->field_count;
                        $ee = $result->fetch_fields();
                        foreach ($ee as $val) {
                            printf("<th class=\"center\">%s</th>\n", $val->name);
                        }
                        echo "</tr>\n</thead>\n";

                        $vv = true;
                        while ($line = $result->fetch_assoc()) {
                            if($vv === true) {
                            echo "<tr>\n";
                            $vv = false;
                            }
                            else{
                            echo "<tr>\n";
                            $vv = true;
                            }
                            foreach ($line as $col_value) {
                            echo "<td>".clean($col_value)."</td>\n";
                            }
                        echo "</tr>\n";
                        }
                        echo "</table>\n";
                        /* Free resultset */
                        $result->free_result();
                    }

Ok, I was able to get a little further. Now, the only thing left that I need help on is the $meta piece:

foreach(range(0, $result->columnCount() - 1) as $column_index)
                        {
                        $meta[] = $result->getColumnMeta($column_index);
                        echo "<th>$meta[0]</th>";
                        }
                        echo "</tr>\n</thead>\n";

                        $vv = true;
                        while ($row = $result->fetch(PDO::FETCH_NUM)) {
                            if($vv === true) {
                            echo "<tr>\n";
                            $vv = false;
                            }
                            else{
                            echo "<tr>\n";
                            $vv = true;
                            }
                            foreach ($row as $col_value) {
                            echo "<td>".clean($col_value)."</td>\n";
                            }
                        echo "</tr>\n";
                        }
                        echo "</table>\n";
                        /* Free resultset */
                        $result->closeCursor();

It is returning the column name as Array instead of the column name.

Nevermind, I finally figured it out. In case anyone is interested in this in the future, here is the final code:

if($type =="query") {

                    $qtext2 = str_replace("\\", " ", $qtext);

                        if($result = DB::inst()->query("$qtext2"))
                            _e( _t( "Successly Executed - " ) );
                        else
                            echo "<font color=red>Not able to execute the query<br>Either the 
                                table doesnot exist or a wrong query.</font><br><br>";

                        _e( _t( "Query is : " ) );
                        echo("<font color=blue>".$qtext2."</font>\n");

                        echo "<table class=\"dynamicTable tableTools table table-striped table-bordered table-condensed table-white\">
                        <thead>
                        <tr>\n";

                        foreach(range(0, $result->columnCount() - 1) as $column_index)
                        {
                        $meta[] = $result->getColumnMeta($column_index);
                        echo "<th>".$meta[$column_index]['name']."</th>";
                        }
                        echo "</tr>\n</thead>\n";

                        $vv = true;
                        while ($row = $result->fetch(\PDO::FETCH_NUM)) {
                            if($vv === true) {
                            echo "<tr>\n";
                            $vv = false;
                            }
                            else{
                            echo "<tr>\n";
                            $vv = true;
                            }
                            foreach ($row as $col_value) {
                            echo "<td>".clean($col_value)."</td>\n";
                            }
                        echo "</tr>\n";
                        }
                        echo "</table>\n";
                        /* Free resultset */
                        $result->closeCursor();
                    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.