将我的 Google Cloud SQL 数据库连接到我托管的 PHP 应用程序

connecting my google cloud sql database, to my hosted php app

提问人:Hamzah Faleel 提问时间:11/3/2023 更新时间:11/3/2023 访问量:26

问:

我正在运行一个项目,我的 PHP 应用程序在其中发出 HTTP 请求,然后它在我的 Google Cloud SQL 数据库中更新该值。我尝试使用公共 IP 进行连接,但遇到同样的错误。

<?php

if(isset($_GET["temperature"])) {
   $temperature = $_GET["temperature"]; // get temperature value from HTTP GET

   $servername = "34.69.43.41";
   $username = "XXXXX";
   $password = "XXXX";
   $database_name = "LeafLink";

   // Create MySQL connection fom PHP to MySQL server
   $connection = new mysqli($servername, $username, $password, $database_name);
   // Check connection
   if ($connection->connect_error) {
      die("MySQL connection failed: " . $connection->connect_error);
   }

   $sql = "INSERT INTO incoming_data (raw_data) VALUES ($temperature)";

   if ($connection->query($sql) === TRUE) {
      echo "New record created successfully";
   } else {
      echo "Error: " . $sql . " => " . $connection->error;
   }

   $connection->close();
} else {
   echo "temperature is not set in the HTTP request";
}
?>

返回错误

这是 PHP 文件。提交请求时,我收到此错误。我是使用 MySQL 和 PHP 的新手,因此非常感谢任何帮助

php google-cloud-platform mysqli 谷歌云 sql

评论

0赞 John Hanley 11/3/2023
您是否将要连接的 IP 地址列入白名单?编辑您的帖子并包含有关您配置的内容的更多详细信息。我还更喜欢编写测试代码来验证数据库连接和与程序分开的命令,以减少依赖性。建议:您的代码不处理异常。
1赞 enocom 11/3/2023
了解运行此代码的位置也会有所帮助。

答: 暂无答案