~南客

导航

php笔记04

PHP+MySQL

连接数据库 mysqli

<?php
		$servername="localhost";
		$username="root";
		$password="******";
		$link=new mysqli($servername,$username,$password);
		if($link->connect_error){
			echo "连接失败";
		}
		echo "连接成功";
		?>

创建表

<?php
		$servername="localhost";
		$username="root";
		$password="******";
		$dbname="user";
		$link=new mysqli($servername,$username,$password,$dbname);
		if($link){
			echo "连接成功!<br>";
		}
		$create="create table user(
			usename INT(10) PRIMARY KEY NOT NULL,
			passwd VARCHAR(30) NOT NULL
		)";
		if($link->query($create)){
			echo "表创建成功!<br>";
		}else{
			echo "表创建失败!<br>";
		}
		$link->close();
		?>

插入数据

<?php
		$servername="localhost";
		$username="root";
		$password="******";
		$dbname="user";
		$link=new mysqli($servername,$username,$password,$dbname);
		if($link){
			echo "连接成功!<br>";
		}
	$usenamex=23324443;
	$passwdx="wfwej.of";
		$sql="insert into user(usename,passwd) values($usenamex,'$passwdx')";
		if($link->query($sql)){
			echo "成功!<br>";
		}else{
			echo "失败!<br>";
		}
		$link->close();
		?>

查询数据

<?php
		$servername="localhost";
		$username="root";
		$password="******";
		$dbname="user";
		$link=new mysqli($servername,$username,$password,$dbname);
		if($link){
			echo "连接成功!<br>";
		}
	$usenamex=23343443;
	$passwdx="wfwej.of";
		$sql=$link->query("select * from user");
		 foreach($sql as $a){
			 echo "id:",$a['id'],"<br>user:",$a['usename'],"<br>passwd:",$a['passwd'];
			 echo "<br><br>";
		 }		
		$link->close();	
		?>

以此类推。。。。。

posted on 2020-02-28 13:19  ~南客  阅读(109)  评论(0编辑  收藏  举报