-1
<?php 
define('DB_NAME', 'xp_database'); // DATABASE
define('DB_USER', 'valli'); // ROOT DEFAULT MYSQL
define('DB_PASSWORD', 'Valli123');  // PASSOWORD
define('DB_HOST', '127.0.0.1'); // LOCAL IF YOU USE LOCAL.
$data=array();
$i=1;
$link = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if($link === false)
{
 die("ERROR: Could not connect. " . mysqli_connect_error());
}
else
{
    echo "Connected to database";
}
 // Attempt select query execution
$sql = "show databases"; 
if($result = mysqli_query($link, $sql))
{ 
    if(mysqli_num_rows($result) > 0)
    { 
        echo "<table>";
        echo "<tr>"; 
        echo "<th>userid</th>";

        echo "</tr>";
        while($row = mysqli_fetch_array($result))
        {
            echo "<tr>"; 
            echo "<td>" . $row['user_id'] . "</td>";
            echo "</tr>";

 // Free result 
 //set mysqli_free_result($result);
        }
    }
    else
    { 
        echo "No records matching your query were found."; 
    } 
} 
else
{ 
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
} 
 // Close connection
 mysqli_close($link);
?>

I have enabled the Admin API in google cloud. Connection is successful. But retrieval using select query is not successful. Please tell me what is missing in this. Is there any other setting that I have to do. I have migrated the data from local database to mysql cloud database.

llinvokerl
  • 1,029
  • 10
  • 25
valli n
  • 55
  • 6
  • 1
    where is your google api connection? – Saf Dec 27 '19 at 07:13
  • google api connection. I dont know about that. Please enlighten me on this. – valli n Dec 27 '19 at 07:38
  • "_But retrieval using select query is not successful._" That's most likely because there is no `select` query in your code. Your query is `show databases`, how do you expect there to be a `user_id` column (from `$row['user_id'] `)? – brombeer Dec 27 '19 at 07:40
  • even for select query it did not work – valli n Dec 27 '19 at 07:49
  • You have a mistake. `$link` can never be false. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) – Dharman Dec 27 '19 at 13:09

1 Answers1

0

If you are using App Engine to connect to Cloud SQL, bear in mind that you will need to enable Cloud SQL proxy since Google App Engine connects from inside of Google servers to your Cloud SQL instance via proxy.

Nonetheless, if what you would like to do is connect from outside with pure PHP code, you can still do it without using the Cloud SQL proxy.

For connecting from outside using pure PHP code you will need to authorize your IP from your Cloud SQL instance, as shown here.

Then, you should modify your code in order to change localhost or 127.0.0.1 to your Cloud SQL instance public IP (taking into account the other variables like username, password and the database name that you would like to connect to).

For finding your Cloud SQL's public IP, you can refer to here.

Finally, if you would like to look around PHP on Google App Engine, you can do it by checking the documentation here.

I hope this helps.

  • I am glad it work out. If you do not want to use the Cloud SQL proxy, you can also still use the public IP from your Cloud SQL instance to make the connection. – Christopher Rodriguez Conde Dec 30 '19 at 08:14
  • i need one more clarification. cloud_sql_proxy works with local system. How to make it work as a website. I deployed the above php in app engine. but it is not getting connected. It says "nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges". Please help me with this. – valli n Dec 30 '19 at 10:03
  • Can anyone give me sample PHP code to connect to mysql in gcp – valli n Dec 30 '19 at 10:35
  • If you would like to use your application outside your local environment, you will need to authorize your Cloud SQL connections to received connections by it's [public IP](https://cloud.google.com/sql/docs/mysql/configure-ip), or [private IP](https://cloud.google.com/sql/docs/mysql/configure-private-ip). – Christopher Rodriguez Conde Dec 31 '19 at 08:46
  • As for the sample PHP code you can take a look at: [Cloud SQL on App Engine Standard for PHP 7.2](https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/appengine/php72/cloudsql), [using Cloud SQL for MySQL in PHP7](https://cloud.google.com/appengine/docs/standard/php7/using-cloud-sql), and a tutorial on [how to build an app with PHP 7.2 and Cloud SQL](https://cloud.google.com/appengine/docs/standard/php7/building-app/). I hope it helps. – Christopher Rodriguez Conde Dec 31 '19 at 08:48