Mysql – Query from 1st domain and insert in 2nd domain

MySQLPHP

I have data in one of my websites. I want to print these data in other site. These two are in same server. At present, I have to export MySQL data and import into other then do this code to insert in 2nd domain.

I want to link directly so that I can make data request from 1st website and show in 2nd website.

I know for this to happen, I need to enable bind-address = 127.0.0.1 to ip address of server and I have changed this.

After that in first website, I have granted permission for that table using this command:

GRANT select ON `database-name.tableA`.* TO 'userB'@'localhost';

but I am getting this error:

Failed to connect to MySQL:
Access denied for user 'userA'@'localhost' to database 'database-name.tableA'

The code I am running from website B:

databaseconnect.php

    <?php
    $con=mysqli_connect("localhost","UserA","passwordA","databaseA");


  if (mysqli_connect_errno($con))
  {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
    ?>

main.php

      <?
                    include('/home/website2/public_html/databaseconnect.php');              
                    $res2 = $con->query("SELECT * FROM `website1_tableA`  where status='0' order by id asc limit 5");
                        while($result = $res2->fetch_assoc()){
                        $cont=$result['content'];
                        $tit=$result['title'];
                            echo $cont;



            }

            ?>

Best Answer

Your GRANT statement seems incorrect.

GRANT select ON `database-name.tableA`.* TO 'userB'@'localhost';

This grants the SELECT privilege on all tables (*) in a database called "database-name.tableA".

The correct statement would be this:

GRANT select ON `database-name`.`tableA` TO 'userB'@'localhost';

Since "database-name.tableA" isn't a valid database, userA can't possibly have permission to access it. When a user tries to grant a permission to another use, the first user must have the privilege they are trying to give to another user, and the first user must have the GRANT OPTION.

You may be asking, "why doesn't the error message tell me there's no such database?"

The reason is because of security. If you don't have permission to access an object, then you also, in most cases, don't have permission to know whether that object exists. If the error message told you the object didn't exist, it would be giving you information that you aren't entitled to know.