**我在做Android与云主机交互时候,由于云主机环境是php所以也就学习了一些php知识,但是遗憾,Android设备测试,没有连上我的云数据库,可是从PHP前台却可以穿进去,我也是很无奈。继续学习吧,所以PHp得数据php数据库的增删改查工具类需要记录一下

2015年10月10日!,找工作?还是考研我很纠结啊?突然我又感冒了,真心窝火难受!懂我么?

**

<?php //连接数据库===================================1
$con = mysql_connect("localhost","peter","abc123");
if (!$con){
die('Could not connect: ' . mysql_error());
}

// some code
mysql_close($con);//关闭数据库;

//创建数据库========================================2创建数据库
$con = mysql_connect("localhost","peter","abc123");
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_close($con);
//创建表==========================================3创建表
$con = mysql_connect("localhost","peter","abc123");
if (!$con){
die('Could not connect: ' . mysql_error());
}

// Create database
if (mysql_query("CREATE DATABASE my_db",$con)){
echo "Database created";
}else{
echo "Error creating database: " . mysql_error();
}
// Create table in my_db database
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);
//向数据库表插入数据==================================4向数据库表插入数据
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire', '33')");
mysql_close($con);
//把来自表单的数据插入数据库==============================5把来自表单的数据插入数据库
<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
$con = mysql_connect("localhost","peter","abc123");
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 "添加了一行!";

mysql_close($con)
//从数据库表中选取数据===================================6从数据库表中选取数据
$con = mysql_connect("localhost","peter","abc123");
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);
//在 HTML 表格中显示结果=================================7在 HTML 表格中显示结果
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
//如需选取匹配指定条件的数据,请向 SELECT 语句添加 WHERE 子句。======8SELECT 语句添加 WHERE 子句。
$con = mysql_connect("localhost","peter","abc123");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'");
while($row = mysql_fetch_array($result)){
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
//ORDER BY 关键词用于对记录集中的数据进行排序================9ORDER BY 关键词
$con = mysql_connect("localhost","peter","abc123");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons ORDER BY age");
while($row = mysql_fetch_array($result)){
echo $row['FirstName'];
echo " " . $row['LastName'];
echo " " . $row['Age'];
echo "<br />";
}
mysql_close($con);
//UPDATE 语句用于中修改数据库表中的数据。================10UPDATE 语句用于中修改数据库表中的数据
$con = mysql_connect("localhost","peter","abc123");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("UPDATE Persons SET Age = '36'
WHERE FirstName = 'Peter' AND LastName = 'Griffin'");
mysql_close($con);

//DELETE FROM 语句用于从数据库表中删除行================DELETE FROM 语句用于从数据库表中删除行
$con = mysql_connect("localhost","peter","abc123");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");
mysql_close($con);
?>

学习可以看看这些

http://jingyan.baidu.com/article/8cdccae9453c3c315513cd49.html

还有一些,但作为学习吧:

http://jingyan.baidu.com/article/da1091fb31319f027949d653.html