Skip to content

Latest commit

 

History

History
53 lines (49 loc) · 1.43 KB

数据库连接及简单使用.md

File metadata and controls

53 lines (49 loc) · 1.43 KB

###mysql连接取值

  $conn =@mysql_connect("localhost","root",'');
  mysql_select_db('chonggu');
  $sql = "select * from cg_case";
  $que = mysql_query($sql);
  while($result = mysql_fetch_array($que))
  {
      print_r($result);
  }

###mysqli连接取值

 $mysql = new mysqli('localhost','root','','chonggu');
 //1.执行语句
 $sql = "delete from limove where id = 221";
 $rst = $mysqli->query($sql);
 //2.获取查询结果 
 $sql = "select * from cg_case ";
 $stmt = $mysql->prepare($sql);
 $stmt->execute();
 $result = $stmt->get_result();
 $array = $result->fetch_all(MYSQLI_ASSOC);
 print_r($array);

###pdo链接数据库

    $dsn = "mysql:dbname=smallprogram;host=localhost;port=3306"; //数据库信息 数据库名,服务器地址,端口
    try {
        $db = new PDO($dsn, "root",'root');   //数据库用户名及密码
    } catch(PDOException $e) {   
    	die('Could not connect to the database:<br/>' . $e);
    }
    //query() 一般用于select语句
    //exec() 一般用于insert update delete语句
    $sql = "select * from navigation order by level asc,id asc";
    $list = $db->query($sql,PDO::FETCH_ASSOC);  //关联数组结果 
    
    /*
    //fetch()只取一条   fetchAll()为全部取出
    while($one = $list->fetch()){   
    	print_r($one);
    }
    */
    $listArr = $list->fetchAll();
    foreach($listArr as $food)
    {
        print_r($food);
    }