Home >php教程 >PHP源码 >数据库连接代码

数据库连接代码

PHP中文网
PHP中文网Original
2016-05-25 17:11:431577browse

数据库连接代码

php中数据库连接代码

<?php 
//连接数据库
$link=mysql_connect("127.0.0.1","root","123456");
//选择数据库
mysql_select_db("userdb",$link);
//sql语句
$sql="select * from userInfo";
//设置查询出来的结果编码集也为utf8
mysql_query("set names utf8");
//执行sql语句
$rs=mysql_query($sql);
//返回受影响的行数
$num=mysql_num_rows($rs);
?>
<table border="1" align="center">
<tr>
<td>姓名</td>
<td>密码</td>
</tr>
<?php
//循环遍历
for($i=0;$i<mysql_num_rows($rs);$i++){
    //将结果集转化为数组
    $list=mysql_fetch_array($rs);
?>
<tr>
<td><?php echo $list["name"]?></td>
<td><?php  echo $list["pwd"]?></td>
</tr>
<?php
}
?>

   

以上就是数据库连接代码的内容,更多相关内容请关注PHP中文网(www.php.cn)!

               

Statement:
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
Previous article:PHP 九九乘法表Next article:反缓存头部信息