Correction status:qualified
Teacher's comments:
<?php //链接数据库mysql //1.创建连接参数另外文件 define('DB_HOST', '127.0.0.1'); define('DB_USER', 'root'); define('DB_PASS', 'root'); define('DB_NAME', 'PHP'); define('DB_CHAR', 'UTF8'); ?>
点击 "运行实例" 按钮查看在线实例
<?php require 'config.php'; //2.导入数据链接函数,成功ture失败false $db = @mysqli_connect(DB_HOST,DB_USER,DB_PASS); //3.测试数据库链接是否成功 if (mysqli_connect_errno($db)) { exit('链接失败'.mysqli_connect_errno($db)); } // echo '链接成功'; //4. 选择要操作的数据库 mysqli_select_db($db, DB_NAME); //5. 设置默认字符集 mysqli_set_charset($db, DB_CHAR);
<?php //单条查询 require 'connect.php'; //查询语句 $sql = "select name from staff"; //执行查询,成功返回结果,失败返回失败原因 $result = mysqli_query($db,$sql); if (mysqli_affected_rows($db) > 0 ) { echo '查询到'.mysqli_affected_rows($db).'条数据'; } else { echo '没有查询到数据'; } while ($row = mysqli_fetch_object($result )){ var_export($row); echo '<br>'; } //释放结果集(仅针对select) mysqli_free_result($result); //关闭数据库连接 mysqli_close($db); ?>
点击 "运行实例" 按钮查看在线实例
点击 "运行实例" 按钮查看在线实例
运行效果图: