当我尝试在创建的数据库中创建表时,我收到A link to the server could not be established in /var/www/Test.php on line 25
和mysql_select_db(): Access denied for user 'pass'@'localhost' (using password: NO) in /var/www/Test.php on line 25
错误。
我的代码:
<?php
$con = mysqli_connect("localhost","root","pass");
connection to mysql server
//checking the connection
if(mysqli_connect_errno($con))
{
echo "Sorry!Failed to connect Mysql" . mysqli_connect_error();
}
$sql = "CREATE DATABASE viral_coff";
if(mysqli_query($con,$sql))
echo "database Created Successfully!";
else
echo "error in creating the database" . mysqli_error($con);
mysql_select_db('viral_coff') or die(mysql_error());;
$tab = ' CREATE TABLE retention( '.
' playing-date DATE ,' .
' that-day INT ,' .
' 1_day INT ,' .
' 3_days INT ,' .
' 7_days INT ,' .
' 31_days INT ,)' ;
if(mysqli_query($con,$tab))
echo "Created Tables";
else
echo "Error creating table" . mysqli_error($con);
mysqli_close($con)
?>
我做错了什么?
答案 0 :(得分:0)
您正在将mysql_*
函数与mysqli_*
函数混合使用。 你当然不能!
您需要使用mysqli_select_db()
代替mysql_select_db()
还要学会在if语句中加上一些括号,这样你就可以100%确定语句内外的内容。
答案 1 :(得分:0)
mysql和mysqli函数不能很好地协同工作。选择其中一个并使用它们,而不是两个。
答案 2 :(得分:0)
您将删除密码。
<?php
define('Hostname','localhost');
define('Username','root');
define('Password','');
define('DB_Name','viral_coff');
$con=mysqli_connect(Hostname,Username,Password);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create database
$sql="CREATE DATABASE ".DB_Name;
if (mysqli_query($con,$sql))
{
echo "Database ".DB_Name." created successfully";
}
else
{
echo "Error creating database: " . mysqli_error($con);
}
$con = mysqli_connect(Hostname,Username,Password,DB_Name);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create table
$sql = "CREATE TABLE retention(playing_date DATE, that_day INT, 1_day INT , 3_days INT ,7_days INT , 31_days INT)";
// Execute query
if (mysqli_query($con,$sql))
{
echo "Table retention created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
?>