php查询mysql数据表记录实现代码
php与mysql是黄金组合,现在我们来讲一下关于php查询mysql数据库记录实现,主要会用到函数mysql_connect mysql_query mysql_select_db mysql_fetch_array四个php函数,下面看实现。
$hostname="localhost"; //定义连接到的mysql服务器名 $username="root"; //定义用于连接的用户名 $password=""; //定义用于连接的密码 $link=mysql_connect($hostname,$username,$password); //打开msql连接 mysql_query('set names gb2312;'); //转换编码以支持中文 mysql_select_db("test"); //选择操作库为test $query="select * from friends"; //定义sql $result=mysql_query($query); //发送sql查询 while($rows=mysql_fetch_array($result)) { echo "id为:".$rows[id]; echo "<br>www.phprm.comn"; echo "name为:".$rows[name]; echo "<br>n"; echo "sex为:".$rows[sex]; echo "<br>n"; echo "birthday为:".$rows[birthday]; echo "<br>n"; echo "address为:".$rows[address]; echo "<p>n"; } //释放结果集 mysql_free_result($result);
//条件查询
$query="select * from friends where id=1"; //定义sql $result= mysql_query($query); //发送sql查询 echo mysql_result($result,0,"name"); //输出name结果 echo "<br>"; echo mysql_result($result,0,"birthday"); //输出birthday结果 echo "<br>"; echo mysql_result($result,0,"sex"); //输出sex结果 echo "<br>"; echo mysql_result($result,0,"address"); //输出address结果
//相关操作
$conn=mysql_connect('localhost','root',''); //打开连接 $fields=mysql_list_fields("test","friends",$conn); //列出test库friends表的信息 $cols=mysql_num_fields($fields); //获取结果数 for($i=0;$i<$cols;$i++) //循环 { echo mysql_field_name($fields,$i)."n"; //输出字段名 echo "<p>"; }
/*
总结
在php中查询数据库记录是相当简单且常用的,只要你记录上面几个函数就可以实现数据查询了。
*/
教程地址:http://www.phprm.com/database/36386.html
欢迎转载!但请带上文章地址^^