php与mysql是黄金组合,现在我们来讲一下关于php查询mysql数据库记录实现,主要会用到函数mysql_connect mysql_query mysql_select_db mysql_fetch_array四个php函数,下面看实现.
- $hostname="localhost";
- $username="root";
- $password="";
- $link=mysql_connect($hostname,$username,$password);
- mysql_query('set names gb2312;');
- mysql_select_db("test");
- $query="select * from friends";
- $result=mysql_query($query);
- while($rows=mysql_fetch_array($result))
- {
- echo "id为:".$rows[id];
- echo "<br>www.phpfensi.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";
- $result= mysql_query($query);
- echo mysql_result($result,0,"name");
- echo "<br>";
- echo mysql_result($result,0,"birthday");
- echo "<br>";
- echo mysql_result($result,0,"sex");
- echo "<br>";
- echo mysql_result($result,0,"address");
-
-
- $conn=mysql_connect('localhost','root','');
- $fields=mysql_list_fields("test","friends",$conn);
- $cols=mysql_num_fields($fields);
- for($i=0;$i<$cols;$i++)
- {
- echo mysql_field_name($fields,$i)."n";
- echo "<p>";
- }
总结:在php中查询数据库记录是相当简单且常用的,只要你记录上面几个函数就可以实现数据查询了. |