php数据库连接及简单操作

 数据库改密码:mysql的控制台mysql console


中文乱码解决方法:原因编码格式不一致
1.建立数据库的时候,字符集选择utf-8
2.修改mysql的配置:在[mysqld]模块下面添加character_set_server=utf8
3.修改页面的编码格式为uft-8,header("content-type:text/html;charset=utf-8");

数据库连接方法1

//造一个连接,需要@ 错误控制运算符 屏蔽错误,这个连接方式将来会被取消
$connect = @mysql_connect("localhost","root","123");

//选择要操作的数据库
mysql_select_db("z-stu",$connect);

//写SQL语句
$sql = "select * from Nation";

//执行SQL语句,返回结果集
$result = mysql_query($sql);

//从结果集中读取数据
while($attr = mysql_fetch_row($result))
{
var_dump($attr);
}

数据库连接方法2

//造一个连接
$con = new MySQLi("localhost","root","","z-stu");

//判断是否有错误

if(mysqli_connect_error()){

  echo "连接失败";

  exit();

}

//写SQL语句

$sql = "select * from Nation";

//执行SQL语句,返回结果集

$result = $con->query($sql);    //->符号表示调用的意思,相当于c#中的点  .  

//从结果集中读取数据
while($a = $result->fetch_row())
{
var_dump($attr);
}

PHP基于数据库连接的简单操作

通过PHP连接数据库实现增删改查的操作:效果图

1、增加数据:

  1、在网页中建一个表单,注意提交的位置,以及提交的方式

 echo "<form action ="tinajia1.php" method="post">

  <input type="text" name="code"/>

  <input type="text" name="name"/>

  <input type="submit" value="添加"/>

  </form>";

  2、在提交的页面中处理,先接收表单的数据,再插入数据库中

    $code = $_POST["code"];

    $name = $_POST["name"];

    $con = @mysql_connect("localhost","root","");

    mysql_select_db("z-stu",$con);

    $sql = "insert into nation values('{$code}','{$name}')";

    $result = mysql_query($sql);

    if($result){

       header("location:chaxun1.php");    //PHP中的页面跳转方式

    }else{

        echo "添加失败";

    }

2、删除数据:

   1、通过超链接提交网页地址的时候加上数据,就是get方式传数据

    <a onclick=" return confirm('确定删除么')" href="delete1.php?code={$a[0]}"><input type="button" value="删除" /></a>

    return是返回confirm的boolean类型的值给《a》标签是否执行

  2.在PHP页面中处理在返回   

    $code = $_GET["code"];

    $con = @mysql_connect("localhost","root","");

    mysql_select_db("z-stu",$con);

    $sql = "delete from nation where code = '{$code}'";

    $result = mysql_query($sql);

    if($result){

      header("location:chaxun1.php");

    }else{

      echo "添加失败";

    }

3、修改数据

  1、超链接到一个表单

   <a href="xiugai.php?code={$a[0]}"><input type="button" value="修改" /></a>

  2、填写修改的内容提交给php处理  

  header("content-type:text/html;charset=utf-8");
  $a = $_GET["code"];
  $con = @mysql_connect("localhost","root","");

  mysql_select_db("z-stu",$con);

  $sql = "select * from nation where code = '{$a}'";

  $result = mysql_query($sql);

  $sttr = mysql_fetch_row($result);

  echo "<form action ="xiugai1.php" method="post">
  <input readonly="readonly" type="text" name="code1" value=" {$sttr[0]} " />
  <input type="text" name="name1" value=" {$sttr[1]} " />
  <input type="submit" value="修改"/>
  </form>";

/*隐藏数据,主键等不可改
style:vis dispaly
type:hidden
readonly="readonly"*/

  3.在PHP页面中处理在返回   

    $b = $_POST["code1"];

    $c = $_POST["name1"];

    $con = @mysql_connect("localhost","root","");

    mysql_select_db("z-stu",$con);

    $sql = "update nation set name = '{$c}' '";


    $result = mysql_query($sql);

    if($result){

      header("location:chaxun1.php");

    }else{

      echo "添加失败";
    }

原文地址:https://www.cnblogs.com/yongjiapei/p/5556794.html