Home > php教程 > php手册 > php查询mysql数据表记录实现代码

php查询mysql数据表记录实现代码

WBOY
Release: 2016-05-26 15:21:52
Original
1704 people have browsed it

php与mysql是黄金组合,现在我们来讲一下关于php查询mysql数据库记录实现,主要会用到函数mysql_connect mysql_query mysql_select_db mysql_fetch_array四个php函数,下面看实现.

<?php
$hostname="localhost";         //定义连接到的mysql服务器名 
$username="root";          //定义用于连接的用户名 
$password="";           //定义用于连接的密码 
$link=mysql_connect($hostname,$username,$password);   //打开msql连接 
mysql_query(&#39;set names gb2312;&#39;);      //转换编码以支持中文 
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(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);       //打开连接 
$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>"; 
}
Copy after login

总结:在php中查询数据库记录是相当简单且常用的,只要你记录上面几个函数就可以实现数据查询了.


本文地址:

转载随意,但请附上文章地址:-)

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template