PHP MySQL重要查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP MySQL重要查询相关的知识,希望对你有一定的参考价值。

//Connection
$con = mysql_connect("localhost","root","password");
mysql_select_db("my_db", $con);

//Creat DB
mysql_query("CREATE DATABASE my_db",$con)

//Create table
$sql = "CREATE TABLE Persons
(
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
FirstName varchar(15) NOT NULL,
LastName varchar(15) NOT NULL,
Age int,
PRIMARY KEY (id)
)";
mysql_query($sql,$con);

//Insert row in table
mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')");
$insertedId=mysql_insert_id();

//Update row
mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'");

//Delete row
mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");

//Select one row
$result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'") or die(mysql_error());
$row = mysql_fetch_array($result);
echo $row['LastName'];

//Select more rows
$result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'") or die(mysql_error());
while( $row = mysql_fetch_array($result) ){
	echo $row['LastName'];
}

//Select more rows order by Age discending from the rows from 0 to 10
$result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter' ORDER BY Age DESC LIMIT 0,10") or die(mysql_error());
while( $row = mysql_fetch_array($result) ){
	echo $row['LastName'];
}

以上是关于PHP MySQL重要查询的主要内容,如果未能解决你的问题,请参考以下文章

使用PHP链接MySQL

MySql

php mysqli扩展之预处理

MySQL重要查询

MySQL 数据库表的查询操作重要

MySQL 数据库表的查询操作重要