Home >php教程 >php手册 >php连接mysql数据库并查询记录所有记录

php连接mysql数据库并查询记录所有记录

WBOY
WBOYOriginal
2016-05-24 18:31:042508browse

下面是一款简单的php操作数据库的程序,我们是先讲php连接mysql数据库,然后再执行sql查询语句再把我们要的记录显示出来,最后关闭与mysql数据库的连接,实例代码如下:

$host = 'localhost'; 
$user_name = 'root'; 
$password = 'admin'; 
$conn = mysql_connect($host,$user_name,$password); 
if(!$conn) 
{ 
    die('数据库连接失败:
'.mysql_error()); 
} 
echo '数据库连接成功!继续操作...'; 
mysql_select_db('test'); 
$sql = 'select id,name,city from users'; 
$result = mysql_query($sql); 
if($result) 
{ 
    echo 'sql语句:' . $sql . '已经成功执行!'; 
    $num = mysql_num_rows($result);    //调用函数mysql_num_row()获得select语句查询结果的行数 
    echo '该sql语句查询到'.$num.'www.phprm.com行数据'; 
}
if(mysql_close($conn)) 
{ 
    echo '
........
'; 
    echo '到数据库的连接已经成功关闭'; 
}


教程链接:

随意转载~但请保留教程地址★

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