1)连接mysql
<?php
$con = mysql_connect("localhost","root","12345");
if(!$con){
die('Could not connect: ' . mysql_error());
}
mysql_close($con);
?>
2)php mysql创建数据库和表
<?php
$con=mysql_connect("localhost","root","123456");
if(!$con){
die('Could not connect: ' . mysql_error()');
}
if(mysql_query("create database my_db",$con));
{
echo "database created";
}else
{
echo "Error create database: '". mysql_error();
}
mysql_close($con);
?>
3)php mysql创建数据库和表
<?php
$con = mysql_connect("localhost","root","123456");
if(!$con){
die('Could not connect: ' . mysql_error()');
}
if(mysql_query("crate database my_db",$con)){
echo "database careated";
}else{
echo "Error create 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);
?>
3)php mysql创建数据库和表
<?php
$con=mysql_connect("localhost","root","123456");
if($con){
die ('Could not connect:' .mysql_error());
}
if(mysql_query("create database my_db",$con));
{
echo "database created";
}else
{
echo "Error create database: '". mysql_error();
}
mysql_select_db("my_db",$con);
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");
mysql_query("NSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire', '33')");
mysql_close($con);
?>
4)php mysql创建表
<?php
$con = mysql_connect("loaclhost","root","123456");
if(!$con){
die('could not conenct:' .mysql_error());
}
//创建数据库
if(mysql_query("create database my_db",$con));
{
echo "database created";
}else
{
echo "Error create database: '". mysql_error();
}
//选择创建的数据库my_db
mysql_select_db("my_db",$con);
//创建表
mysql_select_db("my_db",$con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
mysql_query($sql,$con);
//插入数据
$sql_in ="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if(!mysql_query($sql_n,$con)){
die('Error:' .mysql_error());
}
echo "1 row added";
mysql_close($con);
?>
5)php 查询mysql数据
<?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);
?>
转载于:https://blog.51cto.com/zlymdata/1582227