Connecting to MySQL PHP: 连接Mysql数据库 <? //Create a variable to hold the result of the mysql_connect() function: //Within the die() function, use the mysql_error() function. $connection = mysql_connect("localhost", "root", "") or die(mysql_error()); if ($connection) { $msg = "success!"; } ?> <HTML> <HEAD> <TITLE>MySQL Connection</TITLE> </HEAD> <BODY> <? echo "$msg"; ?> </BODY> </HTML>
Listing Databases on a Server 列出服务器上Mysql的所有的数据库 PHP: <? $connection = @mysql_connect("localhost", "root", "") or die(mysql_error()); $dbs = @mysql_list_dbs($connection)or die(mysql_error()); $db_list ="<ul>"; $i =0; while ($i < mysql_num_rows($dbs)){ $db_names[$i] = mysql_tablename($dbs,$i); $db_list .= "<li>$db_names[$i]"; $i++; } $db_list .="</ul>"; ?> <HTML> <HEAD> <TITLE>MySQL Databases</TITLE> </HEAD> <BODY> <P><strong>Databases on localhost</strong>:</P> <? echo "$db_list"; ?> </BODY> </HTML> 三关键的函数
Listing Tables in a Database 列举处数据库中存在的表.. 从刚才上一节列出的数据库里读取表.. PHP: <? //connection code $connection = @mysql_connect("localhost", "root", "") or die(mysql_error()); //get database list $dbs = @mysql_list_dbs($connection) or die(mysql_error()); //start first bullet list $db_list = "<ul>"; $db_num = 0; //loop through results of function while ($db_num < mysql_num_rows($dbs)) { //get database names and make each a bullet point $db_names[$db_num] = mysql_tablename($dbs, $db_num); $db_list .= "<li>$db_names[$db_num]"; //get table names and start another bullet list $tables = @mysql_list_tables($db_names[$db_num]) or die(mysql_error()); $table_list = "<ul>"; $table_num = 0; //loop through results of function while ($table_num < mysql_num_rows($tables)) { //get table names and make each a bullet point $table_names[$table_num] = mysql_tablename($tables, $table_num); $table_list .= "<li>$table_names[$table_num]"; $table_num++; } //close inner bullet list and increment number to continue loop $table_list .= "</ul>"; $db_list .= "$table_list"; $db_num++; } //close outer bullet list $db_list .= "</ul>"; ?> <HTML> <HEAD> <TITLE>MySQL Tables</TITLE> </HEAD> <BODY> <P><strong>Databases and tables on localhost</strong>:</P> <? echo "$db_list"; ?> </BODY> </HTML>
Creating a New Database 创建新的数据库 这个新的数据库是没带什么表,字段结构... PHP: <? //Create a variable to hold the query to issue, which will create the new database: $sql = "CREATE database testDB2"; $connection = @mysql_connect("localhost", "root", "") or die(mysql_error()); $result = @mysqlquery($sql, $connection) or die(mysql_error()); if ($result) { $msg = "<P>Database has been created!</P>"; } ?> HTML: <HTML> <HEAD> <TITLE>Create a MySQL Database</TITLE> </HEAD> <BODY> PHP: <? echo "$msg"; ?> HTML: </BODY> </HTML>
Deleting a Database 删除数据库 PHP: <? $sql = "DROP DATABASE testDB2"; $connection = @mysql_connect("localhost","root","") or die(mysql_error()); $result = @mysql_query($sql,$connection) or die(mysql_error()); if ($result) { $msg ="<P>Database has been deleted!</P>"; } ?> <HTML> <HEAD> <TITLE>Delete a MySQL Database</TITLE> </HEAD> <BODY> <? echo "$msg"; ?> </BODY> </HTML>
Planning for Your Tables 设计数据库 Creating a table is easy—it's the planning that takes some brainpower. To create a simple table, you only need to give it a name. But that would make for a boring table, because it wouldn't contain any columns (fields) and couldn't hold any data. So besides the name, you should know the number of fields and the types of fields you want to have in your table. PHP: TINYINT A very small integer that can be signed or unsigned. If signed, the allowable range is from 128 to 127. If unsigned, the allowable range is from 0 to 255. SMALLINT A small integer that can be signed or unsigned. If signed, the allowable range is from 32768 to 32767. If unsigned, the allowable range is from 0 to 65535. MEDIUMINT A medium-sized integer that can be signed or unsigned. If signed, the allowable range is from 8388608 to 8388607. If unsigned, the allowable range is from 0 to 16777215. INT A normal-sized integer that can be signed or unsigned. If signed, the allowable range is from 2147483648 to 2147483647. If unsigned, the allowable range is from 0 to 4294967295. BIGINT A large integer that can be signed or unsigned. If signed, the allowable range is from 9223372036854775808 to 9223372036854775808. If unsigned, the allowable range is from 0 to 18446744073709551615. FLOAT A floating point number that cannot be unsigned. You can define the display length (M) and the number of decimals (D). This is not required and will default to 10,2, where 2 is the number of decimals. Decimal precision can go to 24 places for a FLOAT. DATE A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. For example, December 30th, 1973 would be stored as 1973-12-30. DATETIME A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31, plus hour and minute information in HH:MM:SS format. For example, 12:01 AM on December 30th, 1973 would be stored as 1973-12-30 00:01:00. TIMESTAMP A timestamp between midnight, January 1, 1970 and sometime in 2037. You can define multiple lengths to the TIMESTAMP field, which directly correlate to what is stored in it. The default length for TIMESTAMP is 14, which stores YYYYMMDDHHMMSS. This looks like the DATETIME format, only without the hyphens between numbers; 3:30 in the afternoon on December 30th, 1973 would be stored as 19731230153000. Other definitions of TIMESTAMP are 12 (YYMMDDHHMMSS), 8 (YYYYMMDD), and 6 (YYMMDD). CHAR A fixed-length string between 1 and 255 characters in length, right-padded with spaces to the specified length when stored. Defining a length is not required, but the default is 1. VARCHAR A variable-length string between 1 and 255 characters in length. You must define a length when creating a VARCHAR field. BLOB or TEXT A field with a maximum length of 65535 characters. BLOBs are "Binary Large Objects" and are used to store large amounts of binary data, such as images or other types of files. Fields defined as TEXT also hold large amounts of data; the difference between the two is that sorts and comparisons on stored data are case sensitive on BLOBs and case insensitive in TEXT fields. You do not specify a length with BLOB or TEXT. ENUM An enumeration (list). When defining an ENUM, you are creating a list of items from which the value must be selected (or it can be NULL). For example, if you wanted your field to contain either "A" or "B" or "C", you would define your ENUM as ENUM ('A', 'B', 'C') and only those values (or NULL) could ever populate that field. ENUMs can have 65535 different values.
Create a Database Table: Step By Step PHP: <HTML> <HEAD> <TITLE>Create a Database Table: Step 1</TITLE> </HEAD> <BODY> <H1>Step 1: Name and Number</H1> <FORM METHOD="POST" ACTION="do_showfielddef.php"> <P><strong>Table Name:</strong><br> <INPUT TYPE="text" NAME="table_name" SIZE=30></P> <P><strong>Number of Fields:</strong><br> <INPUT TYPE="text" NAME="num_fields" SIZE=5></P> <P><INPUT TYPE="submit" NAME="submit" VALUE="Go to Step 2"></P> </FORM> </BODY> </HTML> Save the file as show_createtable.html.
A Two-Step Form Sequence PHP: <? //validate important input if ((!$_POST[table_name]) || (!$_POST[num_fields])) { header("Location: show_createtable.html"); exit; } //begin creating form for display $form_block = " <FORM METHOD=\"POST\" ACTION=\"do_createtable.php\"> <INPUT TYPE=\"hidden\" NAME=\"table_name\" VALUE=\"$_POST[table_name]\"> <TABLE CELLSPACING=5 CELLPADDING=5> <TR> <TH>FIELD NAME</TH><TH>FIELD TYPE</TH><TH>FIELD LENGTH</TH></TR>"; //count from 0 until you reach the number of fields for ($i = 0; $i <$_POST[num_fields]; $i++) { //add to the form, one row for each field $form_block .= " <TR> <TD ALIGN=CENTER> <INPUT TYPE=\"text\" NAME=\"field_name[]\" SIZE=\"30\"></TD> <TD ALIGN=CENTER> <SELECT NAME=\"field_type[]\"> <OPTION VALUE=\"char\">char</OPTION> <OPTION VALUE=\"date\">date</OPTION> <OPTION VALUE=\"float\">float</OPTION> <OPTION VALUE=\"int\">int</OPTION> <OPTION VALUE=\"text\">text</OPTION> <OPTION VALUE=\"varchar\">varchar</OPTION> </SELECT> </TD> <TD ALIGN=CENTER> <INPUT TYPE=\"text\" NAME=\"field_length[]\" SIZE=\"5\"></TD> </TR>"; } //finish up the form $form_block .= " <TR> <TD ALIGN=CENTER COLSPAN=3><INPUT TYPE=\"submit\" VALUE=\"Create Table\"></TD> </TR> </TABLE> </FORM>"; ?> <HTML> <HEAD> <TITLE>Create a Database Table: Step 2</TITLE> </HEAD> <BODY> <H1>Define fields for <? echo "$_POST[table_name]"; ?></H1> <? echo "$form_block"; ?> </BODY> </HTML> save file as do_createtable.php
Save the file with the name do_createtable.php, and place this file in the document root of your web server. Your code should look something like this: PHP: <? //indicate the database you want to use $db_name = "testDB"; //connect to database $connection = @mysql_connect("localhost", "spike", "9sj7En4") or die(mysql_error()); $db = @mysql_select_db($db_name, $connection) or die(mysql_error()); //start creating the SQL statement $sql = "CREATE TABLE $_POST[table_name] ("; //continue the SQL statement for each new field for ($i = 0; $i < count($_POST[field_name]); $i++) { $sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i]; if ($_POST[field_length][$i] != "") { $sql .= " (".$_POST[field_length][$i]."),"; } else { $sql .= ","; } } //clean up the end of the string $sql = substr($sql, 0, -1); $sql .= ")"; //execute the query $result = mysql_query($sql,$connection) or die(mysql_error()); //get a good message for display upon success if ($result) { $msg = "<P>".$_POST[table_name]." has been created!</P>"; } ?> <HTML> <HEAD> <TITLE>Create a Database Table: Step 3</TITLE> </HEAD> <BODY> <h1>Adding table to <? echo "$db_name"; ?>...</h1> <? echo "$msg"; ?> </BODY> </HTML>