php mysql数据库连接与查询代码

发布时间:2019-11-03编辑:脚本学堂
php mysql数据库连接与查询代码,mysql_connect与mysql_select_db,以及mysql_query、mysql_fetch_array函数用法,需要的朋友参考下。

代码:
 

复制代码 代码示例:
<?php
//create db connection
$connection = mysql_connect("localhost", "user", "password");
if(!$connection)
{
die("Database connection failed:" . mysql_error());
}
 
// select a dtabase to use
$db_select = mysql_select_db("database name", $connection);
if(!$db_select)
{
die("Database selection failed:" . mysql_error());
}
?>
<body>
$result = mysql_query("SELECT * FROM tablename", $connection);
while ($row = mysql_fetch_array($result))
{
   echo $row["rownamehere"]." ".$row["rownamehere"]."<br/>";
}
?>
</body>
<?php
mysql_close($connection);
?>