阅读量:0
Mysql连接
<?php $dbhost = 'localhost'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysqli_error()); } echo '数据库连接成功!'; mysqli_close($conn); ?>
Mysql创建数据库
<?php $dbhost = 'localhost'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接错误: ' . mysqli_error($conn)); } echo '连接成功<br />'; $sql = 'CREATE DATABASE RUNOOB'; $retval = mysqli_query($conn,$sql ); if(! $retval ) { die('创建数据库失败: ' . mysqli_error($conn)); } echo "数据库 RUNOOB 创建成功\n"; mysqli_close($conn); ?>
Mysql删除数据库
<?php $dbhost = 'localhost'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接失败: ' . mysqli_error($conn)); } echo '连接成功<br />'; $sql = 'DROP DATABASE RUNOOB'; $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('删除数据库失败: ' . mysqli_error($conn)); } echo "数据库 RUNOOB 删除成功\n"; mysqli_close($conn); ?>
Mysql选择数据库
<?php $dbhost = 'localhost'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接失败: ' . mysqli_error($conn)); } echo '连接成功'; mysqli_select_db($conn, 'RUNOOB' ); mysqli_close($conn); ?>
Mysql创建数据表
<?php $dbhost = 'localhost'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接失败: ' . mysqli_error($conn)); } echo '连接成功<br />'; $sql = "CREATE TABLE runoob_tbl( ". "runoob_id INT NOT NULL AUTO_INCREMENT, ". "runoob_title VARCHAR(100) NOT NULL, ". "runoob_author VARCHAR(40) NOT NULL, ". "submission_date DATE, ". "PRIMARY KEY ( runoob_id ))ENGINE=InnoDB DEFAULT CHARSET=utf8; "; mysqli_select_db( $conn, 'RUNOOB' ); $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('数据表创建失败: ' . mysqli_error($conn)); } echo "数据表创建成功\n"; mysqli_close($conn); ?>
Mysql删除数据表
<?php $dbhost = 'localhost'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接失败: ' . mysqli_error($conn)); } echo '连接成功<br />'; $sql = "DROP TABLE runoob_tbl"; mysqli_select_db( $conn, 'RUNOOB' ); $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('数据表删除失败: ' . mysqli_error($conn)); } echo "数据表删除成功\n"; mysqli_close($conn); ?>
Mysql插入数据
<?php $dbhost = 'localhost'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接失败: ' . mysqli_error($conn)); } echo '连接成功<br />'; // 设置编码,防止中文乱码 mysqli_query($conn , "set names utf8"); $runoob_title = '学习 Python'; $runoob_author = 'RUNOOB.COM'; $submission_date = '2016-03-06'; $sql = "INSERT INTO runoob_tbl ". "(runoob_title,runoob_author, submission_date) ". "VALUES ". "('$runoob_title','$runoob_author','$submission_date')"; mysqli_select_db( $conn, 'RUNOOB' ); $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('无法插入数据: ' . mysqli_error($conn)); } echo "数据插入成功\n"; mysqli_close($conn); ?>
Mysql查询数据
<?php $dbhost = 'localhost:3306'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接失败: ' . mysqli_error($conn)); } // 设置编码,防止中文乱码 mysqli_query($conn , "set names utf8"); $sql = 'SELECT runoob_id, runoob_title, runoob_author, submission_date FROM runoob_tbl'; mysqli_select_db( $conn, 'RUNOOB' ); $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('无法读取数据: ' . mysqli_error($conn)); } echo '<h2>菜鸟教程 mysqli_fetch_array 测试</h2>'; echo '<table border="1"><tr><td>教程 ID</td><td>标题</td><td>作者</td><td>提交日期</td></tr>'; while($row = mysqli_fetch_array($retval, MYSQLI_NUM)) { echo "<tr><td> {$row[0]}</td> ". "<td>{$row[1]} </td> ". "<td>{$row[2]} </td> ". "<td>{$row[3]} </td> ". "</tr>"; } echo '</table>'; // 释放内存 mysqli_free_result($retval); mysqli_close($conn); ?>
Mysql WHERE 子句
SELECT * FROM users WHERE username = 'test'; SELECT * FROM users WHERE username != 'runoob'; SELECT * FROM products WHERE price > 50.00; SELECT * FROM employees WHERE salary >= 50000; SELECT * FROM products WHERE category = 'Electronics' AND price > 100.00; SELECT * FROM orders WHERE order_date >= '2023-01-01' OR total_amount > 1000.00; SELECT * FROM customers WHERE first_name LIKE 'J%'; SELECT * FROM countries WHERE country_code IN ('US', 'CA', 'MX'); SELECT * FROM products WHERE NOT category = 'Clothing'; SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31'; SELECT * FROM employees WHERE department IS NULL; SELECT * FROM customers WHERE email IS NOT NULL;
Mysql UPDATE 更新
$sql = 'UPDATE table_name SET runoob_title="学习 Python" WHERE runoob_id=3';
MySQL DELETE 语句
$sql = 'DELETE table_name runoob_tbl WHERE runoob_id=3';
MySQL LIKE 子句
$sql = 'SELECT runoob_id, runoob_title, runoob_author, submission_date FROM runoob_tbl WHERE runoob_author LIKE "%COM"';
MySQL UNION 操作符
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合,并去除重复的行。
UNION 操作符必须由两个或多个 SELECT 语句组成,每个 SELECT 语句的列数和对应位置的数据类型必须相同。
//下面的 SQL 语句从 "Websites" 和 "apps" 表中选取所有不同的country(只有不同的值): SELECT country FROM Websites UNION SELECT country FROM appsORDER BY country; //下面的 SQL 语句使用 UNION ALL 从 "Websites" 和 "apps" 表中选取所有的country(也有重复的值): SELECT country FROM Websites UNION ALL SELECT country FROM apps ORDER BY country;
MySQL ORDER BY(排序) 语句
SELECT * FROM employees ORDER BY department_id ASC, hire_date DESC;