【www.gdgbn.com--word】

$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 "
www.111cn.netn";
  echo "name为:".$rows[name];
  echo "
n";
  echo "sex为:".$rows[sex];
  echo "
n";
  echo "birthday为:".$rows[birthday];
  echo "
n";
  echo "address为:".$rows[address];
  echo "

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 "
";
echo mysql_result($result,0,"birthday");      //输出birthday结果
echo "
";
echo mysql_result($result,0,"sex");       //输出sex结果
echo "
";
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 "

";
}


/*
总结

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

本文来源:http://www.gdgbn.com/bangongshuma/28193/