极乐门资源网 Design By www.ioogu.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 数据库操作方法的资料请关注其它相关文章!
极乐门资源网 Design By www.ioogu.com
极乐门资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
极乐门资源网 Design By www.ioogu.com
暂无PHP7原生MySQL数据库操作实现代码的评论...
更新日志
2024年11月13日
2024年11月13日
- 刘欢《雨中的树(新歌加精选)2CD》德国HD24K金碟[WAV+CUE]
- 郑源 《世间情歌》6N纯银SQCD[WAV+CUE][1G]
- 群星《粤潮2HQII》头版限量编号[低速原抓WAV+CUE][991M]
- 群星《2023好听新歌21》十倍音质 U盘音乐[WAV分轨][1G]
- 《热血传奇》双11感恩回馈 超值狂欢30天
- 原神5.2版本活动汇总 5.2版本活动有哪些
- 张敬轩.2010-NO.ELEVEN【环球】【WAV+CUE】
- 黄丽玲.2006-失恋无罪【艾回】【WAV+CUE】
- 阿达娃.2024-Laluna【W8VES】【FLAC分轨】
- 宝可梦大集结段位等级划分表大全 大集结段位一览
- 龙腾世纪影障守护者工坊与装备如何升级 工坊与装备升级说明
- 龙腾世纪影障守护者全成就攻略分享 龙腾世纪4全成就列表一览
- 《剑星》更新四套全新战衣!
- 卡普空老将伊津野英昭宣布入职腾讯光子 开发3A动作
- 38岁梅根·福克斯官宣怀孕:将迎来第四个孩子