这是我检查连接的PHP代码,但它不起作用。请有人帮帮我吗?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$link= new mysql_connect($servername, $username, $password);
if (!$connection) {
echo "<div class=\"alert alert-danger\">";
echo "<span class=\"glyphicon glyphicon-remove-circle\"></span> Connection to MySQL failed: ".mysql_error()."</div>";
mysql_close($link);
exit;
}
// make 'redes' the current db
$db_selected = mysql_select_db('redes', $link);
if (!$db_selected) {
mysql_close($link);
}
?>
答案 0 :(得分:0)
尝试删除new
关键字
$servername = "localhost";
$username = "root";
$password = "";
$link = mysql_connect($servername, $username, $password);// remove `new`
if (!$link) {
echo "<div class=\"alert alert-danger\">";
echo "<span class=\"glyphicon glyphicon-remove-circle\"></span> Connection to MySQL failed: ".mysql_error()."</div>";
mysql_close($link);
exit;
}
查看文档here。
答案 1 :(得分:0)
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = '';
/*** mysql password ***/
$databaseName = 'ads';
// check connection, prepare query and execute it and stop script if something was wrong
// when something was wrong catch error (exception) and show it
// $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// PDOException
try {
$connection = new PDO("mysql:host=$hostname;dbname=$databaseName;charset=utf8", $username, $password);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $exc) {
die("Connection error: " . $exc->getMessage());
}
答案 2 :(得分:0)
让我们试试这个...... 它会起作用
检查第4行我已删除新关键字和if(!$ link)而不是if(!$ connection)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$link = mysql_connect($servername,$username,$password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
echo "<div class=\"alert alert-danger\">";
echo "<span class=\"glyphicon glyphicon-remove-circle\"></span>Connection to MySQL failed: ".mysql_error()."</div>";
mysql_close($link);
?>
答案 3 :(得分:0)
MySQL_
个功能。这些函数已弃用,已在PHP 7中删除。 1)如果您使用的是PHP 7,则您的功能无法正常工作,因为它们无法识别。他们已被删除。无论您运行的是哪个版本的PHP,都使用MySql_
停止。
2)您同时混合使用Object Orientated
和Procedural
语法,这会导致很多错误,就好像您正在和某人用德语交谈一样和法语同时 - 它很难理解并产生错误的语法。
仅使用 One 语法样式;举个例子,这可能是 程序 ,所以你可以这样做:
$link= new mysql_connect($servername, $username, $password);
^^^^^ Remove this word.
if (!$connection) {
永远不会设置$connection
的值,因此if
始终会运行。
从mysql_
转换为mysqli_
程序语法和函数。
请read the documentation for more details。
可以在代码块的开头将数据库的选择组合到连接建立调用中。值得注意的是,最值得注意的是,MySQLi_
函数几乎总是需要引用您想要反馈的连接,例如在此实例中:$link
。因此,mysqli_error()
等进程专门告知哪个连接来提供错误数据。
<?php
$servername = "localhost";
$username = "root";
$password = ''; // password should be in sigle quotes to
// avoid certain strings being accidentally escaped
// or changed such as if your password contains a $ sign.
$db = 'redes';
$link= mysqli_connect($servername, $username, $password, $db);
if (!$link) {
echo "<div class='alert alert-danger'>";
echo "<span class='glyphicon glyphicon-remove-circle'></span>
Connection to MySQL failed: ".mysqli_error($link)."</div>";
mysql_close($link);
exit;
}
//future changing of the database as needed:
$newDb = "newDb";
if(mysqli_select_db ( $link , $newDb)){
print "db changed!";
}