php连接数据库,PHP链接数据库方法

一.链接数据库系统
$con= mysql_connect(servername,username,password)
//1.若链接失败返回FALSE,若成功则在$con中储存链接信息
//2.所有参数要加""
//3. 失败时的反馈命令:
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
二.选择数据库
mysql_select_db(tablename,connection_name)
//connection_name则是上面命令的$con
三.查询
$result = mysql_query(SQL_command)
//mysql_query用于发送SQL命令,返回的是结果。
四.取出查询结果(重点)
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "
";
}
/*
mysql_fetch_array()功能的解释:
the mysql_fetch_array() function returns the first row from the recordset as an array.
Each call to mysql_fetch_array() returns the next row in the recordset.
*/
五.关闭链接:
mysql_close($con);
Tags: 

延伸阅读

最新评论

发表评论