我无法使用php从Google sql云数据库中的表中执行选择记录 [英] I am not able to execute select records from a table in database in google sql cloud using php

查看:64
本文介绍了我无法使用php从Google sql云数据库中的表中执行选择记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我已在Google Cloud中启用了Admin API.连接成功.但是使用选择查询进行检索并不成功.请告诉我这是什么缺失.还有其他我需要做的设置吗?我已将数据从本地数据库迁移到mysql云数据库.

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.

推荐答案

如果您使用App Engine连接到Cloud SQL,请记住,自Google以来,您将需要启用 Cloud SQL代理App Engine通过代理从Google服务器内部连接到您的Cloud SQL实例.

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.

尽管如此,如果您想通过纯PHP代码从外部进行连接,则仍然可以使用 Cloud SQL代理进行操作.

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.

要使用纯PHP代码从外部进行连接,您需要从Cloud SQL实例授权IP,如

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

然后,您应该修改代码,以将 localhost 127.0.0.1 更改为Cloud SQL实例公共IP(请考虑其他变量,例如用户名,密码和您要连接的数据库名称).

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).

要查找Cloud SQL的公共IP,可以在此处>进行引用.

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

最后,如果您想在Google App Engine上浏览PHP,可以通过查看文档

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

我希望这会有所帮助.

这篇关于我无法使用php从Google sql云数据库中的表中执行选择记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆