I have installed the framework cloudframework/appengine-php-core-7.4 following the instruction in cloudframework notion page to develop an API and I need to connect with a database.
What is the best way using this framework?
CodePudding user response:
You ca do it using and object of CloudSQL class. First configure the parameters of your connection
$this->core->config->set('dbUser','<YourUser>');
$this->core->config->set('dbPassword','<YourPassword>');
$this->core->config->set('dbName','<YourDBName>');
// if you are connection outside GCP assign an IP otherwise null
$this->core->config->set('dbServer','<your IP>'|null);
// if you are connecting inside GCP assign a socket otherwise null
$this->core->config->set("dbSocket",'<your socket>'|null);
After that you can create an instance of CloudSQL
/** @var CloudSQL $db */
$db = $this->core->loadClass('CloudSQL');
if(!$db->connect()) return($this->setErrorFromCodelib('system-error',$db->getError()));
// Example of query
$ret = $db->getDataFromQuery('SELECT <yourquery>');
// Return error if the query returns an error
if($db->error()) return($this->setErrorFromCodelib('system-error',$db->getError()));
//now you have in $ret an array with the result of your query