0

I have a problem in my remote server when I used php to connect MySQL using Netbeans

This is my PHP Code:

<?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);
    ?>

I used the given code based on this user's reply to check for error, else it wont show anything.

This is the result:

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 )

*Seriously??

My php is working when I test with phpinfo(), but seems like unable to connect mysql database

The part that confuse me is when I leave the password blank for mysql in phpcode: It show this:

Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'joeslie'@'192.168.1.98' (using password: YES) in /var/www/html/files/config.php on line 11 Connect Error (1045) Access denied for user 'joeslie'@'192.168.1.98' (using password: YES)

So I can say that the PHP still possible to communicate and identify wrong password. Any suggestion?

Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
  • 1
    Looks to me like you connect just fine. Have you tried using the connection to run a statement? – Florian Humblot Jul 27 '17 at 13:04
  • Yes, I am able to execute command with Netbeans, When I enter this query `CREATE DATABASE TestDB1`, it shows `Executed successfully in 0.007 s. 1 row affected. Line 1, column 1 Execution finished after 0.013 s, no errors occurred.` And in my phpmyadmin it added the database –  Jul 27 '17 at 13:07
  • So where did you get the error? – Ankit Singh Jul 27 '17 at 13:07
  • I don't think you have a problem at all. If you remove the password, it can't connect. But as long as you don't actually tell the DB to do anything it won't. Looks like you don't have a problem. – Florian Humblot Jul 27 '17 at 13:08
  • Do you show config.php ? – Anand Pandey Jul 27 '17 at 13:08
  • `if (mysqli_connect_error()) { die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error()); }else{ echo "Sucess"; }` try this line. Does it show `sucess` ? – Vivek Jul 27 '17 at 13:11
  • 1
    It sounds like you're worried about the `mysqli Object ( ...` bit, right? `print_r($connection);` prints out the connection information as an object, so that's where that's coming from. – Chris Forrence Jul 27 '17 at 13:16
  • 1
    your database "test" not allowing "joeslie" to access. so give proper access to this user. – VIRA Jul 27 '17 at 13:21
  • look at your users DB in your Host column. You need there your IP or % as wildcard – A. Blub Jul 27 '17 at 13:41
  • @Vivek I used your code it show success no matter my password correct or not so im not sure. –  Jul 27 '17 at 16:43
  • And as as said earlier my user have the permission to access the server since I am able to make change with MySQL Workbench, not with PHP. –  Jul 27 '17 at 16:44
  • so which error give that in phpmyadmin – Mehul Jariwala Jul 28 '17 at 04:40
  • 1
    So you are able to connnect with database........perform any query for more detail.Like display data or add data or create a table. And also consider @Chris Forrence comment – Vivek Jul 28 '17 at 05:03

1 Answers1

0

Try this and let me know if you are able to create a table or show any error.

 <?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);
  }
  ?>
Vivek
  • 449
  • 18
  • 36