1

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;



            }

            ?>

1 Answer 1

1

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.

2
  • ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 642581 Current database: *** NONE *** Query OK, 0 rows affected (0.05 sec)
    – anamika
    Dec 26, 2014 at 0:17
  • This most likely means the query itself was fine, but your mysql command line client lost connection to the server while it (the client) was sitting idle, either because the server was restarted (or crashed) since the last command you typed, or because the interactive_timeout system variable (default = 8 hours) expired while you were away from the console. It can also occur if your query crashes the server, but that seems unlikely in this case. Dec 26, 2014 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.