Correction status:qualified
Teacher's comments:
数据库连接的基本步骤
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>数据库连接的基本步骤</title> <style type="text/css"> .box {width:90%;line-height:1.2em;border:1px dotted #d6d6d6;padding-left:15px;background-color:#f5f5f5;margin:10px auto;font-family:微软雅黑;} .box1 {width:90%;line-height:1.5em;border:1px dotted #d6d6d6;background-color:#f2edd7;font-family:微软雅黑;} .box p {margin-left:20px;font-size:14px;} hr {height:1px;width:95%;border:none;border-top:1px dashed #666;} </style> </head> <body> <?php echo '<div class="box">'; echo '<h4>数据库连接的基本步骤</h4>'; echo '<div class="box1">'; echo '步骤: * 1. 创建连接参数 * 2. 调用连接函数,返回连接资源 * 3. 判断是否连接成功 * 4. 选择数据库 * 5. 设置默认字符集'; echo '</div>'; echo "<br>"; echo '<div class="box1"> <p>1. 创建连接参数</p>'; echo "<hr>"; echo "<p>define ('DB_HOST', 'localhost') //数据库地址</p>"; echo "<p>define ('DB_USER', 'root') //数据库用户名</p>"; echo "<p>define ('DB_PASS', 'root') //数据库密码</p>"; echo "<p>define ('DB_NAME', 'php') //数据库名</p>"; echo "<p>define ('DB_CHAR', 'utf8') //字符集编码</p>"; echo "<p>【因为连接参数不会经常变化,一般使用常量】</p>"; echo "<br>"; echo '<p>2. 调用连接函数 (成功则返回mysqli对象,失败返回false)</p>'; echo "<hr>"; echo "<p>\$db = @mysqli_connect(DB_HOST, DB_USER, DB_PASS)</p>"; echo "<br>"; echo '<p>3. 测试连接是否成功? (连接失败一定会返回错误编号,可以根据编号判断,也可用 $db是否为false进行判断)</p>'; echo "<hr>"; echo "<p>if (mysqli_connect_errno(\$db)) { exit('连接失败'.mysqli_connect_error(\$db)); }</p>"; echo "<br>"; echo '<p>4. 选择要操作的数据库</p>'; echo "<hr>"; echo "<p>mysqli_select_db(\$db, DB_NAME)</p>"; echo "<br>"; echo '<p>5. 设置默认字符集</p>'; echo "<hr>"; echo "<p>mysqli_set_charset(\$db, DB_CHAR)</p>"; echo "<br>"; echo "<br>"; echo '<p><strong>第234步可以合并写</strong></p>'; echo "<hr>"; echo "<p>\$db = @mysqli_connect(DB_HOST, DB_USER, DB_PASS,DB_NAME) or die('连接失败'.mysqli_connect_error(\$db))</p>"; echo "<br>"; echo '</div>'; echo "<br>"; ?> </body> </html>
点击 "运行实例" 按钮查看在线实例
数据库查询的基本步骤
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>数据库查询的基本步骤</title> <style type="text/css"> .box {width:90%;line-height:1.2em;border:1px dotted #d6d6d6;padding-left:15px;background-color:#f5f5f5;margin:10px auto;font-family:微软雅黑;} .box1 {width:90%;line-height:1.5em;border:1px dotted #d6d6d6;background-color:#f2edd7;font-family:微软雅黑;} .box p {margin-left:20px;font-size:14px;} hr {height:1px;width:95%;border:none;border-top:1px dashed #666;} </style> </head> <body> <?php echo '<div class="box">'; echo '<h4>数据库查询的基本步骤</h4>'; echo '<div class="box1">'; echo '步骤: * 1. 连接数据库 * 2. 执行查询 * 3. 关闭数据库连接'; echo '</div>'; echo "<br>"; echo '<div class="box1">'; echo '<p><strong>知识点:</strong></p>'; echo '<p>* mysqli_query($db, $sql):执行SQL语句</p>'; echo '<p>* mysqli_errno($db):返回最后一次函数执行的错误代码</p>'; echo '<p>* mysqli_error($db):返回最后一次函数执行的错误信息</p>'; echo '<p>* mysqli_close($db):关闭当前的数据连接</p>'; echo '</div>'; echo "<br>"; echo '<div class="box1"> <p>1. 连接数据库</p>'; echo "<hr>"; echo "<p>require 'mysqli_connect.php';</p>"; echo "<p>【require 不是函数,后面不用加括号】</p>"; echo "<br>"; echo '<p>2. 执行查询</p>'; echo "<hr>"; echo "<p>if(\$res = mysqli_query(\$db, \"SELECT name,salary FROM staff\")) { while(\$row = mysqli_fetch_assoc(\$res)) { var_export(\$row); } } else { exit('查询失败'.mysqli_errno(\$db).':'.mysqli_error(\$db))};</p>"; echo "<br>"; echo '<p>释放结果集(仅针对select)</p>'; echo "<hr>"; echo "<p>mysqli_free_result(\$res)</p>"; echo "<br>"; echo '<p>3. 关闭数据库连接</p>'; echo "<hr>"; echo "<p>mysqli_close(\$db)</p>"; echo '</div>'; echo "<br>"; ?> </body> </html>
点击 "运行实例" 按钮查看在线实例