php操作mysql的基本操作,只是简单的增删改查。
<span style="font-size:18px;"><?php
/**
*mysql的操作
*dong
*/
class MysqlOperation{
//连接数据库
static function getConMysql()
{
//mysql_connect(servername,username,password);
/**servername 可选。规定要连接的服务器。默认是 "localhost:3306"。
username 可选。规定登录所使用的用户名。默认值是拥有服务器进程的用户的名称。
password 可选。规定登录所用的密码。默认是 ""。
*/
$con = mysql_connect("localhost","root","tiger");//打开
var_dump($con);
echo '<br/>';
return $con;
}
//关闭数据库
function close($con)
{
echo '<br/>';
var_dump('close:'.mysql_close($con));
}
//创建数据库表
function creatData($dbName,$con)
{
echo "create <br/>";
$sql = "CREATE DATABASE $dbName ";
$result = mysql_query($sql,$con);
var_dump('crdb result'.$result);
}
//创建数据库表
function creatDataTabe($con)
{
$sql = "CREATE TABLE Persons
(
personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
FirstName varchar(15),
LastName varchar(15),
Age int
)";
$result = mysql_query($sql,$con);
var_dump('cdbtable result'.$result);
}
function selectDb($dbName,$con)
{
echo 'selectDb---';
var_dump(mysql_select_db($dbName, $con));
}
//创建数据库表
function insertTabe($con)
{
$sql = "INSERT INTO Persons (FirstName, LastName, Age) VALUES ('zhou','dong',27)";
$result = mysql_query($sql,$con);
echo '<br/>';
var_dump('insert result'.$result);
}
//查询数据库表
function selectTabe($con)
{
$result = mysql_query("SELECT * FROM Persons",$con);
//mysql_query("DELETE FROM Persons WHERE LastName='Griffin'"); //删除数据
//mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'");//修改数据
echo "<br/> selectRe:";
var_dump($result);
while($row = mysql_fetch_array($result))
{
echo "<br />";
echo $row['FirstName'] . " " . $row['LastName'].' '.$row['Age'];
echo "<br />";
}
}
}
function mysqlTest()
{
$mysqlOperation = new MysqlOperation();
$mySql = MysqlOperation::getConMysql();//连接数据库
$dbName = 'php_test';
//$mysqlOperation->creatData($dbName,$mySql);//创建数据库
$mysqlOperation->selectDb($dbName,$mySql);//选择数据库
//$mysqlOperation->creatDataTabe($mySql);//创建数据库表
//$mysqlOperation->insertTabe($mySql);//向表中插入数据
$mysqlOperation->selectTabe($mySql);//向表中插入数据
$mysqlOperation->close($mySql);//关闭连接
}
mysqlTest();//调用函数
?></span>
随风飘!