php连接数据库mysql数据库
查找数据
$con = mysqli_connect('localhost', 'root', '', 'mydb'); if (!$con) { die('数据库连接失败' . mysqli_connect_error()); } else { echo '数据库连接成功'; } $sql='SELECT * FROM mysable'; //$result=mysqli_query($con,$sql); $result=$con->query($sql);//面对对象 print_r($result); while($row=$result->fetch_assoc()){ print_r($row) ; }
方法2
header('content-type:text/html;charset :utf-8'); $host='localhost'; $user='root'; $pwd=''; $dbname='hxq'; $link=mysqli_connect($host,$user,$pwd); if(!$link){ echo "连接数据库失败"; } //选则数据库操作 $db=mysqli_select_db($link,$dbname); if(!$db){ echo '连接数据库失败'; } //设置mysql 返回数组字符集 mysqli_query($link,"set names utf8"); //执行sql语句 $sql='select * from 007_news ORDER BY id ASC limit 0,20'; $result=mysqli_query($link,$sql);
while($row=mysqli_fetch_assoc($result)){ $str=' <tr>'; $str.=" <td> $row[id] </td>"; $str.=" <td> $row[title] </td>"; $str.=" <td> $row[author] </td>"; $str.=" <td> $row[source] </td>"; $str.=" <td> $row[hits] </td>"; $str.=" <td> ".date('Y-m-d H:i:s',$row['addate'])." </td>"; $str.=" <td> <a href='javascript:;'>修改</a> | <a href='javascript:;'>删除</a> </td>"; $str.="</tr>"; echo $str; }