PHP读取SQL数据库实例教程
2018-3-31 乱云飞 代码 评论(0) 浏览(991) 标签: 读取SQL数据库 php代码
<?php //PHP读取SQL数据库 $con = mysql_connect("localhost","root","123456");//连接数据库 if (!$con){ die("数据库服务器连接失败"); } //如果出错提示 @mysql_select_db("dbname", $con);//数据库名字 mysql_query("set names 'utf8'");//避免中文乱码 $sql = "SELECT * FROM emlog_blog"; //定义变量,选取表 $result = mysql_query($sql); //执行SQL语句,获得结果集 //下面是打印输出,样式随意 echo "<table border=1>"; while( $row = mysql_fetch_array($result) )//逐行获取 { $gid= $row['gid']; $title = $row['title']; $date = $row['date']; echo "<tr>"; echo "<td>$gid</td>"; echo "<td>$title</td>"; echo "<td>$date</td>"; echo "</tr>"; } echo "<table />"; ?>