Chris Fremgen
Chris Fremgen

Reputation: 5358

Google Cloud SQL Insert Not working?

I am just starting out with Cloud SQL, so I'm sure there is a simple mistake I'm making.

I am able to connect to my Cloud instance via MySQL Workbench. I created a database and table, and was able to insert and select from this table from MySQL Workbench.

However, when I try to connect through my app engine, I am getting an error.

EDITED- Here's my code:

<?php
try{
    $db = null;
    $db = new pdo('mysql:unix_socket=/cloudsql/my-gae-appname:my-cloud-sql-instance;dbname=myschema', 'root', '');

    mysql_select_db('myschema');

    $action = $_GET["action"];
    $mapid = $_GET["mapid"];
    $src = $_GET["src"];
    $ip = $_SERVER["REMOTE_ADDR"];

    $stmt = $db->prepare('INSERT INTO myschema.report (action, mapid, ip, source) VALUES (?,?,?,?)');
    $stmt->execute(array($action, $mapid, $ip, $src));
    $affected_rows = $stmt->rowCount();

    echo $affected_rows;

    $db = null;

}catch(PDOException $ex){
    echo $ex->getMessage();
  }
?>

The error I get is: SQLSTATE[HY000] [1049] Unknown database 'dpe'

Thanks!

Upvotes: 0

Views: 1122

Answers (1)

Siniseus
Siniseus

Reputation: 121

mysql_select_db(); has been deprecated and you should be using the class object PDO exclusively in the case. You can also use the MYSQLI class.

try{
$db = null;
$db = new pdo('mysql:unix_socket=/cloudsql/my-gae-appname:my-cloud-sql-instance;dbname=myschema', 'root', '');

$action = $_GET["action"];
$mapid = $_GET["mapid"];
$src = $_GET["src"];
$ip = $_SERVER["REMOTE_ADDR"];

$stmt = $db->prepare('INSERT INTO myschema.report (action, mapid, ip, source) VALUES (?,?,?,?)');
$stmt->execute(array($action, $mapid, $ip, $src));
$affected_rows = $stmt->rowCount();

echo $affected_rows;

$db = null;

}catch(PDOException $ex){
echo $ex->getMessage();
}

Upvotes: 2

Related Questions