0
votes

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!

1
try to add error repoting on your php error_reporting(E_ALL); ini_set('display_errors', '1'); and $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); below the connection - Kevin
Probably because you're mixing MySQL APIs using mysql_select_db and PDO. - Funk Forty Niner
Usual PDO connect syntax $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); - as per php.net/manual/en/pdo.connections.php - So, why are you using mysql_select_db('myschema');? - Funk Forty Niner
@Fred-ii- please see my response to Siniseus's answer. - Chris Fremgen

1 Answers

2
votes

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();
}