douwei1128
douwei1128
2018-05-17 11:07
浏览 147
已采纳

无法使用php连接到服务器

<?php
$host="localhost";
$user="user";
$password="debashree";
$connect=mysqli_connect($host,$user,$password);
if ($connect->connect_error) {
 die("connection_aborted".$connect->connect_error);
}
echo "connected succesfully";

This is the mysql connection php script. It is constantly showing the error of

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'user'@'localhost' (using password: YES) in C:\xampp\htdocs\index.php on line 6

Notice: Trying to get property of non-object in C:\xampp\htdocs\index.php on line 7 connected successfully

I cannot understand please help me?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpnzf48660
    dpnzf48660 2018-05-17 11:27
    已采纳

    You should try the following changes made :

    <?php
    try{
    $host="localhost";
    $user="user";
    $password="debashree";
    $databaseName = "myDB";
    $connect=mysqli_connect($host,$user,$password, $databaseName);
    if (**mysqli_connect_errno()**)
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    echo "connected succesfully";
    }catch(Exception $e){
    echo $e->getMessage();
    }
    

    Also, mysqli_connect requires database as fourth parameter.

    mysqli_connect("localhost","my_user","my_password","my_db");

    点赞 评论

相关推荐