如何使用PHP从Google Cloud SQL连接和检索数据? [英] How do I connect and retrieve data from Google Cloud SQL using PHP?

查看:90
本文介绍了如何使用PHP从Google Cloud SQL连接和检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能找到的唯一教程使用了陌生的替代资源,例如应用程序引擎,作曲家,github,代理,其他插件.

使用纯PHP不能简单地连接到它吗?

例如,在下面的代码中,我需要修改以从Google Cloud SQL获取数据吗?

 <?php$ servername ="localhost";$ username =用户名";$ password =密码";$ dbname ="myDB";//创建连接$ conn = mysqli_connect($服务器名,$用户名,$密码,$ dbname);//检查连接如果(!$ conn){die(连接失败:".mysqli_connect_error());}$ sql =从MyGuests选择SELECT ID,名字,姓氏";$结果= mysqli_query($ conn,$ sql);如果(mysqli_num_rows($ result)> 0){//输出每一行的数据while($ row = mysqli_fetch_assoc($ result)){回声"id:".$ row ["id"]."- 名称: " .$ row ["firstname"].".$ row ["lastname"].< br>";}} 别的 {回显"0结果";}mysqli_close($ conn);?> 

解决方案

Google App Engine通过代理从Google服务器内部连接到Cloud SQL实例,但是 Cloud SQL实例只是一个托管的MySQL实例.因此,如果您只想使用PHP代码从外部连接而没有代理,那么您只需要授权IP(在这里: http://console.cloud.google.com/sql/instances/

但是,如果您仍然想在Google App Engine上浏览PHP,请检查该链接

Google App Engine connects from inside of Google servers to the Cloud SQL instance via proxy, but the Cloud SQL instance is just a managed MySQL instance. So, if you want just to connect from the outside with PHP code and without a proxy, then you just need to authorize your IP (here: https://cloud.google.com/sql/docs/mysql/connect-external-app#appaccessIP) in the Cloud SQL instance.

And then, you should modify the code to change "localhost" to your Google Cloud SQL instance public IP (with its proper username, password and dbname). You can find the public IP here: http://console.cloud.google.com/sql/instances/

But, if you still want to have a look around PHP on Google App Engine, check that link https://cloud.google.com/appengine/docs/standard/php/cloud-sql/using-cloud-sql-mysql

这篇关于如何使用PHP从Google Cloud SQL连接和检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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