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 数据库操作方法的资料请关注其它相关文章!
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月29日
2024年11月29日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]