码迷,mamicode.com
首页 > 数据库 > 详细

PHP操作mysql数据库

时间:2014-11-25 11:01:35      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:php   mysql   

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);

?>


PHP操作mysql数据库

标签:php   mysql   

原文地址:http://zlymdata.blog.51cto.com/6104904/1582228

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!