重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
?php
网站建设哪家好,找创新互联公司!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了肃宁免费建站欢迎大家使用!
$con = mysql_connect("localhost", "hello", "321");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("test_db", $con);
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysql_error());
}
mysql_close($con);
?
其实就是先登录再选择数据库
mysql_select_db
?php
$data = array();
$db_name_php = 'books';
if (!mysql_connect('localhost', 'root', '123456')) {
echo '不能连接到mysql';
exit;
}
$result = mysql_query('show databases;');
While($row = mysql_fetch_assoc($result)){
$data[] = $row['Database'];
}
unset($result, $row);
mysql_close();
print_r($data);
echo 'brbr'; if (in_array(strtolower($db_name_php), $data))
echo '[',$db_name_php,']数据库存在';
else
echo '[',$db_name_php,']数据库不存在';
?
form action="" method="post"
select name="host"
option value="localhost" selectedlocalhost/option
option value="127.0.0.1"127.0.0.1/option
/select
brbr
user:input type="text" name="user" value=""brbr
pwd :input type="passWord" name="pwd" value=""brbr
input type="submit" value="connent"
input type="reset" value="reset"
/form
?php
error_reporting(~E_ALL);
$host = $_POST['host'];
$user = $_POST['user'];
$pwd = $_POST['pwd'];
if(isset($_POST['host']) isset($_POST['user']) isset($_POST['pwd'])){
if(strlen($host)1 or strlen($user)1 or strlen($pwd)1){
echo "请完善相关数据库链接信息。";
exit(0);
}
$conn = mysql_connect($host, $user, $pwd) or die("Error-数据库连接失败!");
if($conn){
echo "OK—数据库连接成功!";
}
}
?