无为清净楼资源网 Design By www.qnjia.com

php7中mysql的连接与使用与PHP5中大不相同

PHP5中mysql_connect()等函数大多被PHP7中类的成员函数所代替。PHP5中连接mysql是过程性的,而PHP7中强调了类的使用与面向对象的方法

$user = new mysqli();

//Connect to mysql
$user->connect("localhost", "root", "q721@Ms67", "Student");

mysql的连接被mysqli类所替代

//Search in mysql
$query = 'SELECT * FROM users';
//Use a variable to save result
$result = $user->query($query);

查询语句也变成了类的成员函数

连接到 MySQL服务器

mysqli_connect(host, username, password [,dbname] [,port]);
- 参数:
host:MySQL服务器。可以包含端口号,默认值为“localhost:3306”
username:用户名。默认值是服务器进程所有者的用户名;
password:密码。
dbname:数据库名称。
port:MySQL服务器的端口号,默认为3306。
- 返回值:如果连接成功,则返回 mysqli 连接对象。如果失败,则返回 false。

实例代码一

<"localhost", "root", "password", "testgame");
 if(!$mysqli) {
  echo"database error";
 }else{
  echo"php env successful";
 }
 $mysqli->close();
""这样哦

代码二

<"Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
exit;
}else
echo '数据库连接上了!';

/* Close the connection 关闭连接*/
mysqli_close($link);
"htmlcode">
<"localhost";
$username = "root";
$password = "root";

$dbname = "myDB";

// 创建连接

# Tip: 如果你使用其他端口(默认为3306),为数据库参数添加空字符串,如: new mysqli("localhost", "username", "password", "", port)
$conn = new mysqli($servername, $username, $password,$dbname);

// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "连接成功";

$conn->close();
"htmlcode">
$sql = "CREATE DATABASE myDB3";
if (mysqli_query($conn, $sql)) {
echo "数据库创建成功";
} else {
echo "Error creating database: " . mysqli_error($conn);
}

方法二:

// 检测连接
if ($conn->connect_error) {
  die("连接失败: " . $conn->connect_error);
}
echo "连接成功";

$sql = "CREATE DATABASE myDB2";
if ($conn->query($sql) === TRUE) {
  echo "数据库创建成功";
} else {
  echo "Error creating database: " . $conn->error;
}

创建表的方法:

方法一:

// 使用 sql 创建数据表
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if (mysqli_query($conn, $sql)) {
echo "数据表 MyGuests 创建成功";
} else {
echo "创建数据表错误: " . mysqli_error($conn);
}

方法二:

// 使用 sql 创建数据表
$sql = "CREATE TABLE MyGuests2 (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "创建数据表错误: " . $conn->error;
}

插入数据的方法:

方法一:

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if (mysqli_query($conn, $sql)) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

方法二:

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

插入多条数据的方法:

方法一:

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')";

if (mysqli_multi_query($conn, $sql)) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

方法二:

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('22', 'Doe', 'john@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('22', 'Moe', 'mary@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('22', 'Dooley', 'julie@example.com')";

if ($conn->multi_query($sql) === TRUE) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

查询数据库的方法:

方法一:

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// 输出数据
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 结果";
}

方法二:

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 结果";
}

看到这里小编再为大家分享一个整理好的类库

<"Can not connect to MySQL server");
  }
  
		if($this->server_info() > '5.0'){
			mysqli_query($this->link, "SET sql_mode=''");
		}
		/*
		if($dbname) {
			if (!@mysqli_select_db($dbname, $this->link)){
				$this->halt('Cannot use database '.$dbname);
			}
		}*/
	}

	function select_db($dbname) {
		$this->dbname = $dbname;
		if (!@mysqli_select_db($dbname, $this->link)){
			$this->halt('Cannot use database '.$dbname);
		}
	}

	function server_info() {
		return mysqli_get_server_info($this->link);
	}
	
	function version() {
		return mysqli_get_server_info($this->link);
	}
	
	
	function insert($tableName, $column = array()) {
   $columnName = "";
   $columnValue = "";
   foreach ($column as $key => $value) {
    $columnName .= $key . ",";
    $columnValue .= "'" . $value . "',";
   }
   $columnName = substr($columnName, 0, strlen($columnName) - 1);
   $columnValue = substr($columnValue, 0, strlen($columnValue) - 1);
   $sql = "INSERT INTO $tableName($columnName) VALUES($columnValue)";
   $this->query($sql);
  }
	 
	function update($tableName, $column = array(), $where = "") {
   $updateValue = "";
   foreach ($column as $key => $value) {
    $updateValue .= $key . "='" . $value . "',";
   }
   $updateValue = substr($updateValue, 0, strlen($updateValue) - 1);
   $sql = "UPDATE $tableName SET $updateValue";
   $sql .= $where " WHERE $where" : null;
   $this->query($sql);
  }
	 function delete($tableName, $where = ""){
   $sql = "DELETE FROM $tableName";
   $sql .= $where " WHERE $where" : null;
   $this->query($sql);
  }
	 function select($tableName, $columnName = "*", $where = "") {
   $sql = "SELECT " . $columnName . " FROM " . $tableName;
   $sql .= $where " WHERE " . $where : null;
   $this->query($sql);
  }
	 function get_all($sql,$result_type = MYSQLI_ASSOC) {
  $query = $this->query($sql);
  $i = 0;
  $rt = array();
  while($row =& mysqli_fetch_array($query,$result_type)) {
   $rt[$i]=$row;
   $i++;
  }
  //$this->write_log("获取全部记录 ".$sql);
  return $rt;
 }


 function fetchRow($query){
  return mysqli_fetch_assoc($query);
 }
	
	function query($sql) {
  //$this->write_log("查询 ".$sql);
		mysqli_query($this->link,"set names utf8");
  $query = mysqli_query($this->link,$sql);
  //if(!$query) $this->halt('Query Error: ' . $sql);
  return $query;
 }
	//获取第一个字段值
 function getOne($sql, $limited = false){
  if ($limited == true){
   $sql = trim($sql . ' LIMIT 1');
  }

  $res = $this->query($sql);
  if ($res !== false){
   $row = mysqli_fetch_row($res);

   if ($row !== false){
    return $row[0];
   }else{
    return '';
   }
  }else{
   return false;
  }
 }
	
	
	function fetch_array($query, $result_type = MYSQLI_ASSOC) {
  return mysqli_fetch_array($query, $result_type);
 }
	
	//输出记录
	function fetch_first($sql) {
		$res=$this->query($sql);
		return $this->fetch_array($res,MYSQLI_ASSOC);
	}
	
	// 取得一条数据记录
	function get_one($sql, $result_type = MYSQLI_ASSOC){
		$result = $this->query($sql);
		$record = $this->fetch_array($result, $result_type);
		return $record;
	}

 function getRow($sql, $limited = false){
  if ($limited == true){
   $sql = trim($sql . 'LIMIT 1');
  }

  $res = $this->query($sql);
  if ($res !== false){
   return mysqli_fetch_assoc($res);
  }else{
   return false;
  }
 }

 
 //取影响条数 
	function affected_rows() {
		return mysqli_affected_rows($this->link);
	}
	//从结果集中取得一行作为枚举数组 
	function fetch_row($query) {
		return mysqli_fetch_row($query);
	}
	// 结果条数
	function num_rows($query) {
		return mysqli_num_rows($query);
	}
	// 取字段总数 
	function num_fields($query) {
		return mysqli_num_fields($query);
	}
	// 返回查询结果
	function result($query, $row) {
		$query = mysqli_result($query, $row);
		return $query;
	}
	//释放结果集 
	function free_result($query) {
		return mysqli_free_result($query);
	}
	//返回自增ID 
	function insert_id() {
		return ($id = mysqli_insert_id($this->link)) >= 0 "SELECT last_insert_id()"), 0);
	}


	function close() {
		return mysqli_close($this->link);
	}

 function error() {
  return (($this->link) "<html>\n<head>\n";
		$msg .= "<meta content=\"text/html; charset=$charset\" http-equiv=\"Content-Type\">\n";
		$msg .= "<style type=\"text/css\">\n";
		$msg .= "body,p,pre {\n";
		$msg .= "font:12px Verdana;\n";
		$msg .= "}\n";
		$msg .= "</style>\n";
		$msg .= "</head>\n";
		$msg .= "<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#006699\" vlink=\"#5493B4\">\n";
		$msg .= "<b>error</b>: ".htmlspecialchars($this->error())."\n<br />";
		$msg .= "<b>error number</b>: ".$this->errno()."\n<br />";
		$msg .= "<b>Date</b>: ".date("Y-m-d @ H:i")."\n<br />";
		$msg .= "<b>Script File</b>: http://".$_SERVER['HTTP_HOST'].getenv("REQUEST_URI")."\n<br />";

		$msg .= "</body>\n</html>";
		echo $msg;
		exit;
	}
}
?>

 以上就是PHP7原生MySQL数据库操作实现代码的详细内容,更多关于php7 数据库操作方法的资料请关注其它相关文章!

标签:
php7,数据库操作,php7,数据库

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。