我有这个代码应该将我的网站连接到我在xampp中创建的数据库。
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'anyons');
define('DB_USER','root');
define('DB_PASSWORD','');
$field1_name=$_POST['email'];
$field2_name=$_POST['pass'];
$field3_name=$_POST['name'];
$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD)or die("Failed to connect to
MySQL: " . mysql_error());
@mysqli_select_db(DB_NAME,$con) or die( "Unable to select database");
$query = "INSERT INTO login VALUES('$field1_name','$field2_name',
'$field3_name')";mysql_query($query);mysql_close();
?>
我收到错误无法选择数据库,这意味着某处连接断开。我一直想弄清楚,但我真的不能。帮助将受到高度赞赏。
答案 0 :(得分:0)
您已合并mysqli
和mysql
按照以下代码替换您的代码
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'anyons');
define('DB_USER','root');
define('DB_PASSWORD','');
$con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
// connection checking
if (mysqli_connect_errno()) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
// filtering values
extract($_POST); // collected all post values
$email = mysqli_real_escape_string($con, $email);
$pass = mysqli_real_escape_string($con, $pass);
$name = mysqli_real_escape_string($con, $name);
$query = "INSERT INTO login (email, pass, name) VALUES ('{$email}','{$pass}','{$name}')";
if(!mysqli_query($con, $query)){
die('Error: ' . mysqli_error($con));
}else{
echo 'records added!';
}
mysqli_close($con);
?>