Getting Started...LOGIN

Getting Started with PHP - Creating a New Database

For the database, we have two ways to create it. One is to use code to create it.

The code is as follows:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>create database</title>
</head>
<body>
	<?php
		$con = mysql_connect("localhost","root","root") or die("数据库连接失败");
		$sql ="CREATE DATABASE test1";
		$info = mysql_query($sql);
		if($info){
			echo "创建成功";
		}else{
			echo "创建失败";
		}
	?>
</body>
</html>

Note: $con is to link the database $ info To execute the sql statement

Run this code, and then check the database. If the database already exists, it cannot be created

The second is to enter the URL http://localhost /phpMyAdmin/

Then enter the user and password to create the database on a simple page


to the database Delete operation

You can enter the database and delete the table visually. The visualization will be more intuitive

Use code to delete, the code is as follows:

DROP DATABASE Delete the database

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>drop database</title>
</head>
<body>
	<?php
		$con = mysql_connect("localhost","root","root") or die("数据库连接失败");

		$sql ="DROP DATABASE test1";

		$info = mysql_query($sql);

		if($info){
			echo "删除成功";
		}else{
			echo "删除失败";
		}
	?>
</body>
</html>

Run the code, and then check the database to see if the database has been deleted

Create mysql data table

You can create a table visually or code. The code is as follows:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>创建 MySQL 数据表</title>
</head>
<body>
<?php
	$dbhost = 'localhost';
	$dbuser = 'root';
	$dbpass = 'root';
	$conn = mysql_connect($dbhost, $dbuser, $dbpass);
	if(! $conn )
	{
	  die('连接失败: ' . mysql_error());
	}
	echo '连接成功<br />';
	$sql = "CREATE TABLE php_tbl( ".
	       "php_id INT NOT NULL AUTO_INCREMENT, ".
	       "php_title VARCHAR(100) NOT NULL, ".
	       "php_author VARCHAR(40) NOT NULL, ".
	       "submission_date DATE, ".
	       "PRIMARY KEY ( php_id )); ";
	mysql_select_db( 'php' );
	$retval = mysql_query( $sql, $conn );
	if(! $retval )
	{
	  die('数据表创建失败: ' . mysql_error());
	}
	echo "数据表创建成功\n";
	mysql_close($conn);
?>
</body>
</html>

Delete data table

Visual operation is a convenient way. You can also use a script to delete the code as follows:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>删除 MySQL 数据表</title>
</head>
<body>
	<?php
		$dbhost = 'localhost';
		$dbuser = 'root';
		$dbpass = 'root';
		$conn = mysql_connect($dbhost, $dbuser, $dbpass);
		if(! $conn )
		{
		  die('连接失败: ' . mysql_error());
		}
		echo '连接成功<br />';
		$sql = "DROP TABLE php_tbl";
		mysql_select_db( 'php' );
		$retval = mysql_query( $sql, $conn );
		if(! $retval )
		{
		  die('数据表删除失败: ' . mysql_error());
		}
		echo "数据表删除成功\n";
		mysql_close($conn);
	?>
</body>
</html>

Note: Due to server security considerations, database-related codes are not available online. For testing, you can copy the code and test it locally on your computer

Next Section
<?php echo "欢迎学习数据库知识"; ?>
submitReset Code
ChapterCourseware