Home > php教程 > php手册 > php mysql 查询数据库

php mysql 查询数据库

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-02 09:14:28
Original
805 people have browsed it

文章提供一款简单的mysql 查询数据库数据的代码,可以查询blog里面所有数据并且显示出来,代码如下:

<?php 
session_start(); 
$conn=mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;)or die(&#39;数据库连接错误&#39;); 
mysql_select_db(&#39;demo&#39;,$conn); 
mysql_query("set names &#39;gbk&#39;"); 
$us=$_session[&#39;username&#39;]; 
$selequery=mysql_query("select * from blog where author=&#39;$us&#39; order by id desc")or die("查询失败"); 
?> 
<?php while($selerow=mysql_fetch_array($selequery)){?> 
<form action="" method="post" name="modform"> 
  <table width="100%" border="1"> 
    <tr> 
      <td width="100">作者:</td> 
      <td><?php echo $_session[&#39;username&#39;]."         ".$selerow[&#39;redate&#39;];?></td> 
    </tr> 
    <tr> 
      <td width="100">留言标题:</td> 
      <td><?php echo $selerow[&#39;title&#39;];?></td> 
    </tr> 
    <tr> 
      <td>留言内容:</td> 
      <td><?php echo $selerow[&#39;content&#39;];?></td> 
    </tr> 
      <tr> 
      <td colspan="2" align="center"><input type="submit" value="修改" name="sub"></input></td> 
    </tr> 
  </table> 
 </form>  
  <br> 
  <?php }?>  
<!--<?php  
 for($i=1;$i<$selerow=mysql_num_rows($selequery);$i++){ 
  if($i<$selerow=mysql_num_rows($selequery)){?> 
  //开源代码phpfensi.com 
   <table><tr><td><?php echo $selerow[&#39;title&#39;];?></td></tr></table> 
   <hr color="red"></hr> 
  <?php  
  }else{?> 
  <table></table> 
<?php  
 }} 
?>
Copy after login

实例数据库代码如下:

drop table if exists `blog`; 
create table `blog` ( 
  `id` int(10) not null auto_increment, 
  `author` varchar(50) not null, 
  `title` varchar(50) not null, 
  `content` text, 
  `redate` date not null, 
  primary key (`id`) 
) engine=innodb auto_increment=5 default charset=gbk; 
-- ---------------------------- 
-- records of blog 
-- ---------------------------- 
insert into `blog` values (&#39;1&#39;, &#39;admin&#39;, &#39;我&#39;, &#39;<p>呜呜呜</p>&#39;, &#39;2010-07-31&#39;); 
insert into `blog` values (&#39;2&#39;, &#39;admin&#39;, &#39;第五&#39;, &#39;<p>我的</p>&#39;, &#39;2010-07-31&#39;); 
insert into `blog` values (&#39;3&#39;, &#39;admin&#39;, &#39;我的&#39;, &#39;<p>去权威的物权法</p>&#39;, &#39;2010-07-31&#39;); 
insert into `blog` values (&#39;4&#39;, &#39;111111&#39;, &#39;大武器&#39;, &#39;<p>的弯曲的千万</p>&#39;, &#39;2010-07-31&#39;);
Copy after login


本文地址:

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

Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template