我目前拥有Google的Cloud SQL数据库和Namecheap的Web / PHP服务器。我正在尝试使用我的Namecheap服务器中的PHP脚本来连接数据并将数据插入到我的Cloud SQL DB中的表中。我的PHP脚本适用于我的Namecheap帐户提供的数据库,但不适用于我为新的Cloud SQL连接创建凭据和主机更改。我知道我可以通过我的IP建立连接并且它们是正确的,因为我可以从MySQL Workbench连接到Cloud SQL DB。
错误:
[11-Oct-2017 19:28:12 America/New_York] PHP Warning: mysqli_connect():
(HY000/2003): Can't connect to MySQL server on '35.199.10.53' (111
"Connection refused") in
/home/dumamwny/public_html/php/googlemule.php on line 11
[11-Oct-2017 19:28:12 America/New_York] PHP Notice: Trying to get property
of non-object in /home/dumamwny/public_html/php/googlemule.php on line 26
[11-Oct-2017 19:28:12 America/New_York] PHP Fatal error: Uncaught Error:
Call to a member function query() on string in
/home/dumamwny/public_html/php/php2.php:16
Stack trace:
#0 {main}
thrown in /home/dumamwny/public_html/php/php2.php on line 16
欢迎任何建议,以下是我的代码。
googlemule.php
<?php
function db_connect() {
// Define connection as a static variable, to avoid connecting more than once
static $muleconnection;
// Try and connect to the database, if a connection has not been established yet
if(!isset($muleconnection)) {
// Load configuration as an array. Use the actual location of your configuration file
$config = parse_ini_file('/home/dumamwny/private/googlemule.ini');
$muleconnection = mysqli_connect($config['servername'],$config['username'],$config['password'],$config['dbname']);
}
// If connection was not successful, handle the error
if($muleconnection === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
return mysqli_connect_error();
}
return $muleconnection;
}
// Connect to the database
$muleconnection = db_connect();
// Check connection
if ($muleconnection->connect_error) {
die("Connection failed: " . $muleconnection->connect_error);
}
?>
php2.php
<?php
require_once('googlemule.php');
$charName = $_POST['charName'];
$logType = $_POST['logType'];
$magicPrice = $_POST['magicPrice'];
$magicAmount = $_POST['magicAmount'];
$yewPrice = $_POST['yewPrice'];
$yewAmount = $_POST['yewAmount'];
$totalWealth = $_POST['totalWealth'];
$sql = "INSERT INTO dumaismule (char_name, log_type, magic_log_price,
magic_log_amount, yew_log_price, yew_log_amount, total_wealth)
VALUES ($charName, $logType, $magicPrice, $magicAmount, $yewPrice,
$yewAmount, $totalWealth)";
$query = $muleconnection->query($sql);
echo 'OK..';
?>