php连接mysql公共文件、操作mysql数据库
//php连接mysql公共文件
<?php //数据库配置信息 $db_host = "localhost";//主机名 $db_user = "admin";//用户名 $db_pass = "admin";//密码 $db_name = "tablename";//数据库名 $charset = "utf8";//字符集 //php连接mysql服务器 if (!$link = @mysqli_connect($db_host, $db_user, $db_pass)) { echo "<h2>数据库连接失败!</h2>"; die(); } //选择数据库 if (!mysqli_select_db($link, $db_name)) { echo "<h2>数据库选择失败!</h2>"; die(); } //设置字符集对象 mysqli_set_charset($link, $charset);
//php执行mysql语句
//定义sql语句 $sql = "SELECT * FROM tablename"; //执行结果 //$link=mysqli_connect($db_host, $db_user, $db_pass)连接数据库成功后 $result = mysqli_query($link, $sql); //结果导入到数组中 //MYSQLI_ASSOC为常量 $arrs = mysqli_fetch_all($result, MYSQLI_ASSOC) //获取记录数目 $records = mysqli_num_rows($result);
//mysql 查询数据
$sql = "SELECT * FROM photos WHERE username = '$username' ORDER BY id DESC LIMIT {$startrow}, {$pagesize}";
//mysql 更新数据
$sql = "UPDATE photos SET hits = {$hits} WHERE imgsrc = {$src}";