继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

PHP操作mysql数据库

慕村225694
关注TA
已关注
手记 240
粉丝 7
获赞 27

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

?>


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP