"PHP additions and Deletions Instance" section fourth-DIY a database management tool

Source: Internet
Author: User
Tags php server

This section describes how to DIY a database management tools, you can enter SQL on the page to make simple additions and deletions to change the operation.

First, locate the XAMPP installation directory and open the Htdocs:

Create a new PHP file with the name mysqladmin.php

1. Write PHP Server code 1.1 write the PHP tag

First, or on this page, to write PHP code, you need to have a PHP tag:

Our PHP code should be written in this tab.

1.2 Database connection Operations

XAMPP installed MySQL default no password, do not write on the line.

1.3 Getting the form table only son over SQL statements

1.4 Execute the passed SQL statement with the mysql_query function

So far, the code is enough to make additions and deletions to the database, and then we'll design the implementation of the query SQL.

1.5 split the SQL statement with the split function to get the table name

1.6 Use the table name to get all the columns of this table, and assemble the column names in a number.

1.7 Go to query the result set obtained in SQL, show to page
if ($tableName) {$query = mysql_query ("Select column_name from INFORMATION_SCHEMA. COLUMNS WHERE table_name = ' $tableName '; ") Or Die ("<p style= ' color:red ' >sql errors, error message ======>". Mysql_error (). "                         </p> "); Traverse the result set--Mysql_fetch_array $columns = Array (); Store all field names in this table $count = 0;                        Record the current subscript echo "<table>";                        echo "<tr>";                                while ($row = Mysql_fetch_array ($query)) {$columns [$count] = $row ["column_name"]; echo "<td>". $row ["column_name"].                                "</td>";                            $count = $count + 1;                        } echo "</tr>";                                                                        echo sizeof ($COLUMNS); $query _02 = mysql_query ($sql) or Die ("<p style= ' color:red ' >sql errors, error message ======>". Mysql_error (). "                                    </p> ");                while ($row = mysql_fetch_array ($query _02)) {echo "<tr>"; for ($i =0; $i <sizeof ($columns), $i + +) {echo <td>. $row [$columns [$i]].                "</td>";                            } echo "</tr>";                                } echo "</table>";  }            }

The demo works as follows:

Access Address: http://localhost:8080/mysqladmin.php

The Complete mysqladmin.php code:

<form action= "mysqladmin.php" method= "POST" > <textarea cols= "Up" rows= "ten" id= "SQL" name= "SQL" ></texta    rea> <br> <input type= "Submit" value= "execute"/> </form><style> th {background: #eaeaea}    TD {BORDER:1PX Solid #ccc;p adding:2px 10px;}    Tr:hover {background:skyblue}</style><?php//1, connection database $conn = mysql_connect ("localhost", "root", "");    2. Select Test Database $db = mysql_select_db ("Test", $conn);        3, set the encoding set mysql_query ("Set names UTF8");    $sql = "";        if (isset ($_post["SQL") && $_post["SQL"]! = null) {$sql = $_post["SQL"];           echo "<p> you executed sql: $sql </p>"; echo "<script>document.getelementbyid (' SQL '). value= '". $sql. "        </script> "; Start the real execution of SQL $query _origin = mysql_query ($sql) or Die ("<p style= ' color:red ' >sql ' error message," ======> ". Mysql_err or (). "        </p> "); echo "<p style= ' Color:green ' > executed successfully!        </p> ";            /*If it is an UPDATE statement, returns a number if it is a SELECT statement, then returns a result set, such as: Resource ID #6 *///echo $query;        Echo Strpos ($query. "", ' Resource ');                Select column_name from Information_schema. ' COLUMNS ' WHERE table_name = ' tm_users ';        Gets the table name according to the SQL statement $arr = Split ("", $sql);        $tableName = "";                for ($i =0; $i <sizeof ($arr), $i + +) {if ($arr [$i] = = "from") {$tableName = $arr [$i +1];            Break        }}//echo $tableName; If the table name exists, query the table for all fields if ($tableName) {$query = mysql_query ("Select column_name from INFORMATION_SCHEMA. ' COLUMNS ' WHERE table_name = ' $tableName ' ") or Die (" <p style= ' color:red ' >sql "error message ======>". Mysql_error (). "                        </p> ");            $columns = Array ();            $count = 0;            echo "<table cellpadding=0 cellspacing=0><tr>";                                  while ($row =mysql_fetch_array ($query)) {$columns [$count + +] = $row [' column_name ']; }//traversal array for ($i =0; $i <sizeof ($columns); $i + +) {echo <th> $columns [$i]</t            H> ";            } echo "</tr><tr>"; Then go to this table and query while ($row =mysql_fetch_array ($query _origin)) {//Iterate through the table's column names and then take the value from the row to F or ($i =0; $i <sizeof ($columns); $i + +) {echo <td>. $row ["$columns [$i]"].                "</td>";            }//Each line is traversed, the echo "</tr>" is changed;        } echo "</table>"; }}?>

"PHP additions and Deletions Instance" section fourth-DIY a database management tool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.