PHP无法连接MySQL,但能够识别密码

时间:2017-07-27 13:01:41

标签: php mysql

当我使用php使用Netbeans

连接MySQL时,我的远程服务器出了问题

这是我的PHP代码:

<?php
    $dbPassword = "********";
    $dbUserName = "********";
    $dbServer = "192.168.1.98"; //Remote Server with LAMP
    $dbName = "test";

    ini_set('display_errors',1);
    error_reporting(E_ALL ^ E_NOTICE);

    $connection = new mysqli($dbServer,$dbUserName,$dbPassword,$dbName);
    if (mysqli_connect_error()) {
      die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
    }

    print_r($connection);
    ?>

我使用基于this用户回复的给定代码来检查错误,否则它不会显示任何内容。

结果如下:

mysqli Object ( [affected_rows] => 0 [client_info] => mysqlnd 5.0.12-dev 
- 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $ 
[client_version] => 50012 [connect_errno] => 0 [connect_error] => [errno] => 0
[error] => [error_list] => Array ( ) [field_count] => 0 [host_info] =>
 192.168.1.98 via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.7.19-
0ubuntu0.16.04.1 [server_version] => 50719 [stat] => Uptime: 4794 Threads: 5 
Questions: 1574 Slow queries: 0 Opens: 339 Flush tables: 1 Open tables: 166 
Queries per second avg: 0.328 [sqlstate] => 00000 [protocol_version] => 10 
[thread_id] => 182 [warning_count] => 0 )

*认真??

当我使用phpinfo()进行测试时,我的php正在工作,但似乎无法连接mysql数据库

令我困惑的部分是当我在phpcode中为mysql留下密码为空时:它显示了这个:

  

警告:mysqli :: __ construct():( HY000 / 1045):拒绝用户访问   &#39; joeslie&#39; @&#39; 192.168.1.98&#39; (使用密码:YES)in   第11行的/var/www/html/files/config.php        连接错误(1045)拒绝访问用户&#39; joeslie&#39; @&#39; 192.168.1.98&#39; (使用密码:是)

所以我可以说PHP仍然可以进行通信并识别错误的密码。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

试试这个,如果您能够创建表格或显示任何错误,请告诉我。

 <?php
  $dbPassword = "********";
  $dbUserName = "********";
  $dbServer = "192.168.1.98"; //Remote Server with LAMP
  $dbName = "test";

  ini_set('display_errors',1);
  error_reporting(E_ALL ^ E_NOTICE);

  $connection = new mysqli($dbServer,$dbUserName,$dbPassword,$dbName);
  if($connection){
   echo "Connected to database.";
  }else{
   die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
  }

  // sql to create table
  $sql = "CREATE TABLE tableName(
   id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
   firstname VARCHAR(30) NOT NULL,
   lastname VARCHAR(30) NOT NULL,
   email VARCHAR(50),
   reg_date TIMESTAMP
  )";

  if (mysqli_query($connection , $sql)) {
   echo "Table tableName created successfully";
  } else {
   echo "Error creating table: " . mysqli_error($conn);
  }
  ?>