Create and delete data tables in the MySQL database

Source: Internet
Author: User

 

<?
$ Link = mysql_connect ("localhost", "root ","");
Mysql_query ("use MySQL ");
$ My = "create table test1 (Serial int unsigned not null primary key auto_increment, Id varchar (20) not null, passwd varchar (20 ))";
Mysql_query ($ my, $ link );
$ DATA = "insert into test1 values ('', 'john', '123 ')";
$ Data2 = "insert into test1 values ('', 'def', '123 ')";
Mysql_query ($ data );
Mysql_query ($ data2 );

$ Result = mysql_query ("select * From test1", $ link );
While ($ ROW = mysql_fetch_row ($ result ))
{
For ($ I = 0; $ I <count ($ row); $ I ++)
{
Echo "$ row [$ I] \ n". "<br> ";
}

 

 

<? PHP
/*************************************** ******
*
* Tutorial name: PHP Program Design tutorial (Lujiang Youyu)
* Author: Lujiang Youyu PHPMA jacking
* Email: phpma@163.com wenlongjie@163.com
* Address: http://www.phpma.com blog: http://blog.phpma.com Forum: http://bbs.phpma.com
* Help, problem, suggestion: http://bbs.phpma.com/thread-htm-fid-2.html
* This program is a sample tutorial carefully prepared by Lujiang Youyu and jacking. The production process is the process of our growth, I hope our tutorials can help more people quickly learn PHP programs, so that more people can understand open source, understand open source, spread open source, and contribute to the open source business...
* During the test, please refer to the corresponding notes and make corresponding modifications.
*
**************************************** *******/
?>
<HTML>
<Head>
<Title> Create a table in the database </title>
</Head>
<Body>
<? PHP
// After data is created, create a data table in the database and return the list of data tables cyclically...
$ Db_host = localhost; // MySQL Server Name
$ Db_user = root; // MySQL Username
$ Db_pass = ""; // password of the MySQL user
$ Db_name = "test"; // database to be operated
// Use the mysql_connect () function to connect to the server. If an error occurs, the corresponding information is returned.
$ Link = mysql_connect ($ db_host, $ db_user, $ db_pass) or die ("cannot connect to server". mysql_error ());
Mysql_select_db ($ db_name, $ link); // select the database. Here, select the test database.
// The following $ SQL statement is the SQL statement used to create a table.
$ SQL = "CREATE TABLE test1 (ID int (5) not null auto_increment primary key, name varchar (12) not null, mail varchar (30) not null, phone varchar (14) not null, address varchar (30) not null )";
If (mysql_query ($ SQL, $ link) // send an SQL statement to create a table.
Echo "Table test1 created successfully"; // information displayed if the table is created successfully
Echo "<p> ";
Echo "all data tables on current". $ db_name. "are :";
Echo "<p> ";
$ Table_list = mysql_list_tables ($ db_name, $ link); // display tables in the database
While ($ ROW = mysql_fetch_row ($ table_list) // cyclically traverses the returned result set
{
Echo $ row [0]; // displays the table name
Echo "<p> ";
}
?>
</Body>
</Html>

 

<HTML>
<Head>
<Title> delete a table from the database </title>
</Head>
<Body>
<? PHP
// How to use the PHP program to delete data tables... example + comment describes everything...
$ Db_host = localhost; // MySQL Server Name
$ Db_user = root; // MySQL Username
$ Db_pass = ""; // password of the MySQL user
$ Db_name = "test"; // database to be operated
// Use the mysql_connect () function to connect to the server. If an error occurs, the corresponding information is returned.
$ Link = mysql_connect ($ db_host, $ db_user, $ db_pass) or die ("cannot connect to server". mysql_error ());
Mysql_select_db ($ db_name, $ link); // select the database. Here, select the test database.
Echo "all data tables on current". $ db_name. "are:"; // The table is displayed after deletion for comparison.
Echo "<p> ";
$ Table_list = mysql_list_tables ($ db_name, $ link); // display tables in the database
While ($ ROW = mysql_fetch_row ($ table_list) // cyclically traverses the returned result set
{
Echo $ row [0]; // displays the table name
Echo "<p> ";
}
$ SQL = "Drop table test1"; // Delete the SQL statement of a table
If (mysql_query ($ SQL, $ link) // execute an SQL statement
Echo "Table test1 has been deleted! ";
Echo "<p> ";
Echo "all data tables on current". $ db_name. "are:"; // display the deleted table again for comparison
Echo "<p> ";
$ Table_list = mysql_list_tables ($ db_name, $ link); // display tables in the database
While ($ ROW = mysql_fetch_row ($ table_list) // cyclically traverses the returned result set
{
Echo $ row [0]; // displays the table name
Echo "<p> ";
}
?>
</Body>
</Html>

}

 


?>

Related Article

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.