Sample code for PHP 5 to manage any database table in MySQL 5 (1)

Source: Internet
Author: User
PHP 5 provides sample code for managing any database tables in MySQL 5 (1). Read the sample code for managing any database tables in MySQL 5 (1 ), PHP5 + Mysql5 + Apache2 is now the most popular website creation mode. It is too cumbersome to operate Mysql tables only using the SQL language. Therefore, I will refer to "> <LINKhref

PHP5 + Mysql5 + Apache2 is now the most popular website creation mode. It is too cumbersome to operate Mysql tables only using the SQL language. Therefore, I wrote a set of php code for adding, deleting, and modifying records of various tables by referring to the code on the Internet. For your reference and entertainment only.

The following code is successfully debugged on PHP5 + Mysql5 + Apache2 for WinXp.

First, describe the applicability of the code. according to the characteristics of most tables, each table has an id. Therefore, the code applies to the table with the first column id. As for what the name does not matter, it is important that the code uses the first column as the primary key by default. There are no other restrictions. this code can operate any column table. Disadvantage: no input review is conducted. Therefore, do not enter blank rows or mismatched types when entering the record. this must be used with Mysql table creation.

Note: reminder! When copying the above code, be sure to remove the tab before each line of code in the editor. otherwise, php5 cannot be compiled.
In addition, $ database in each file is currently sunsite, and $ table is software, which can be modified as needed.

The first is index. php. The main function is to allow users to enter the table name, and then click the View button to view the table content. It is also the default homepage.

// Index. php



Admin

Please input the table name



Then tables. php is used to display the table content. With the entry for adding, deleting, and modifying.
// Tables. php


Current table


$ Database = "sunsite"
$ Tablename = $ _ REQUEST ['tablename'];
Echo "Data from $ tablename"
Mysql_connect ("localhost", "root", "") or die ("Problem connecting to DataBase ");
$ Query = "show columns from $ tablename"
$ Result = mysql_db_query ($ database, $ query );
$ Column = 0;
If ($ result)
{
Echo "Found these entries in the database:

"
Echo"






$ Query = "select * from $ tablename"
$ Result = mysql_db_query ($ database, $ query );
If ($ result)
While ($ r = mysql_fetch_array ($ result ))
{
Echo"





"While ($ r = mysql_fetch_array ($ result )){Echo" "$ Column = $ column + 1;}Echo" "Mysql_free_result ($ result ); "For ($ col = 0; $ col <$ column; $ col ++) echo" "Echo" "}Echo"
$ R [0]
$ R [$ col]
"
}
Else echo "No data ."
Mysql_free_result ($ result );
If ($ column! = 0) // ($ tablename = "software" | $ tablename = "techtalk ")
Echo"



  • Home
  • Add a new entry
  • Edit an entry
  • Delete an entry
"
?>

To be continued.

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.