PHP之mysql数据库

WBOY
Release: 2016-06-23 13:39:40
Original
1081 people have browsed it

1.连接数据库

mysql -u root -p
Copy after login

2.显示有几个数据库

show databses
Copy after login

3.创建数据库

<?php $con = mysql_connect ( "localhost", "root", "a76d290e04" );	if (! $con) {		die ( 'Could not connect: ' . mysql_error () );	}		if (! $con) {		die ( 'Could not connect: ' . mysql_error () );	}		if (mysql_query ( "CREATE DATABASE my_db", $con )) {		echo "Database created";	} else {		echo "Error creating database: " . mysql_error ();	}		mysql_select_db ( "my_db", $con );	$sql = "CREATE TABLE Persons 	(	FirstName varchar(15),	LastName varchar(15),	Age int	)";		mysql_query ( $sql, $con );		mysql_close ( $con );	  ?>
Copy after login

二.插入数据

<?php $con = mysql_connect ( "localhost", "root", "a76d290e04" );	if (! $con) {		die ( 'Could not connect: ' . mysql_error () );	}		if (! $con) {		die ( 'Could not connect: ' . mysql_error () );	}		mysql_select_db ( "my_db", $con );		$sql = "INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";		if (! mysql_query ( $sql, $con )) {		die ( 'Error: ' . mysql_error () );	}	echo "1 record added";		mysql_close ( $con );?>
Copy after login
Firstname: Lastname: Age:


三.查询数据

<?php $con = mysql_connect ( "localhost", "root", "a76d290e04" );	if (! $con) {		die ( 'Could not connect: ' . mysql_error () );	}		if (! $con) {		die ( 'Could not connect: ' . mysql_error () );	}		mysql_select_db ( "my_db", $con );		$result = mysql_query("SELECT * FROM Persons");	while ( $row = mysql_fetch_array ( $result ) ) {		echo $row ['FirstName'] . " " . $row ['LastName'];		echo "<br />";}			mysql_close ( $con );?>
Copy after login



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template