I am trying to create a simple php application on heroku with postgresql back end.
I have created a connection to postgresql server with the provided credentials from heroku:-
<?php
class DB_Connect
{
private $db;
public function connect()
{
$host = "";
$port = "port=5432";
$dbname = "";
$credentials = "user= password=";
$db = pg_connect( " $url $host $port $dbname $credentials" );
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
}
}
$db1 = new DB_Connect();
$conn = $db1->connect();
$query = "insert into public.user_test(name,email) values('val','val#in.com')";
$result = pg_query($conn,$query);
echo $result;
?>
When i deploy the app on heroku, I get a Opened Database successfully message but nothing gets inserted into the table when I check the table using pgadmin.
I tried it with and without the schema name "public", but that didn't help either.
When I run the app locally by providing the local postgresql credentials, the table gets populated.
You didn't return the connection in your connect method. Adding return $db; after the if condition should do the trick.
class DB_Connect
{
public function connect()
{
$host = "";
$port = "port=5432";
$dbname = "";
$credentials = "user= password=";
$db = pg_connect( " $url $host $port $dbname $credentials" );
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
return $db;
}
}
Related
I´m trying to connect Cloud SQL with App engine this way
<?php
class ConectorBD{
private $host = 'localhost';
private $user = 'first_user';
private $password = '12345';
private $port = null;
private $socket = '/cloudsql/instance-name';
private $connect;
function initConnect($name_db){
$this->connect = new mysqli($this->host, $this->user, $this->password, $name_db, $this->port, $this->socket);
if ($this->connect ->connect_error) {
return "Error:" . $this->conexion->connect_error;
}else {
return "OK";
}
}
}
$con = new ConectorBD();
echo $con->initConnect('my_db');
?>
But the next error appear Error:MySQL server has gone away
To get the connection name open Cloud Shell and run command below
gcloud sql instances describe instance-name
$conn = new PDO("mysql:unix_socket=/cloudsql/project_id:sql_instance_region:intance_id;dbname=db_name", "root", "your_root_password");
$mysqli = mysqli_connect(null, "root" , "your_root_password", 'db_name', null, '/cloudsql/project_id:sql_instance_region:intance_id');
I am having problems connecting with a database using a php form. I have created a separate file for php database parameters.
<?
$db_username = "username";
$db_password = "password";
$db_name = "db";
$db_host = "host";
?>
Include statement
include "db_data.php";
This is my connection String
$dblink = $Database->connect($db_username, $db_password, $db_name, $db_host);
This is my database class
<?php
class Database{
var $dblink;
var $query_error;
var $username;
var $password;
var $host;
var $database;
function connect ($username, $password, $dbname, $host) {
$this->dblink = mysql_connect($host, $username, $password);
mysql_select_db($dbname);
if($this->dblink == ""){
echo "Error connecting to database $database<Br>";
return -1;
}else{
return $this->dblink;
}
}
function disconnect(){
mysql_close($this->dblink);
}
}
?>
I haven't changed any of my connection parameters. Will starting again mysql server solve the problem ??
Thanks Jose
A better way to connect to a database is this
mysqli_connect("database ip", "database name", "username for database", "password");
Try that and place it in a file named connection.php and put the code above in a var like such :
$conn = mysqli_connect("database ip", "database name", "username for database", "password");
Then include the file in your PHP files so there's no need to re-write this in every file
Include("connection.php");
I am trying to have a database created for each user that signs up for my web app. This database will be based on a template to store their data.
The problem I'm running into is that Google Could SQL doesn't seem to be creating the Database via PHP and MySQL queries.
Connecting to the Instance works fine and triggers no errors based on this code:
//CLOUD SQL CONNECTION FOR SUBSCRIPTIONS
$hostname = details;
$username = details;
$password = details;
$port = details;
$socket = details;
$connection = new mysqli($hostname, $username, $password, $port, $socket);
// Check Connection
if($connection->connect_error) {
trigger_error('Connection failed: ' . $connection->connect_error, E_USER_ERROR);
}
However, when I go to create a simple database, mysqli fails without an error...:
//Create Database
$username = 'account';
$database = 'sub_'. $username .'_db';
$query = "CREATE DATABASE IF NOT EXISTS `{$database}`;";
if(mysqli_query($connection, $query)) {
echo "Database my_db created successfully";
} else {
echo "Error creating database: " . mysqli_error($connection);
}
The output of this is simply: Error creating database:
Am I missing something? How do I create a database in a Cloud SQL instance via PHP? Thanks for any input.
Can anyone confirm that this can actually be done on GAE PHP and Cloud SQL?
It seems the $connection variable was actually NULL despite not throwing an error. This worked for me though:
I connected to an existing database:
//CLOUD SQL CONNECTION FOR SUBSCRIPTIONS
$hostname = details;
$username = details;
$password = details;
$db = details;
$port = details;
$socket = details;
$connection = new mysqli($hostname, $username, $password, $db, $port, $socket);
// Check Connection
if($connection->connect_error) {
trigger_error('Connection failed: ' . $connection->connect_error, E_USER_ERROR);
}
Then I created a new database:
//Create Database
$username = 'account';
$database = 'sub_'. $username .'_db';
$query = "CREATE DATABASE IF NOT EXISTS `{$database}`;";
if(mysqli_query($connection, $query)) {
echo "Database my_db created successfully";
} else {
echo "Error creating database: " . mysqli_error($connection);
}
Then used mysqli_select_db() to select the newly created database.
//Select Newly Created DB
mysqli_select_db($connection, $database);
I am new to PHP and need to modify some code in order to compile with my Microsoft SQL Server. The original code is like this. I downloaded it from usercake
<?php
/*
UserCake Version: 2.0.2
http://usercake.com
*/
//Database Information
$db_host = "localhost"; //Host address (most likely localhost)
$db_name = "202"; //Name of Database
$db_user = "202"; //Name of database user
$db_pass = "password"; //Password for database user
$db_table_prefix = "uc_";
GLOBAL $errors;
GLOBAL $successes;
$errors = array();
$successes = array();
/* Create a new mysqli object with database connection parameters */
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
GLOBAL $mysqli;
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
//Direct to install directory, if it exists
if(is_dir("install/"))
{
header("Location: install/");
die();
}
?>
I already installed sqlsrv and tested the link. It works with my database. Then I changed the code to this:
<?php
//Database Information
$server = "servername";
$connectionInfo = array("Database"=>"databasename","UID"=>"xxxxxx", "PWD"=>"xxxxxx" );
$db_table_prefix = "uc_";
GLOBAL $errors;
GLOBAL $successes;
$errors = array();
$successes = array();
/* Create a new sqlsrv object with database connection parameters */
$mssqlsrv = new sqlsrv($server, $connectionInfo);
GLOBAL $mssqlsrv;
if(sqlsrv_connect_errno()) {
echo "Connection Failed: " . sqlsrv_connect_errno();
exit();
}
//Direct to install directory, if it exists
if(is_dir("install/"))
{
header("Location: install/");
die();
}
?>
I get the following error message:
Fatal error: Class 'mssql' not found in
I think this line is the problem:
$mssqlsrv = new sqlsrv($server, $connectionInfo);
But I do not know how to fix this.
I would use PDO in this case: http://www.php.net/manual/en/pdo.construct.php
You can create a DSN connection to SQL Server
$dsn = "sqlsrv:Server=servername;Database=databasename"
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
The information to connect to SQL server is available here: http://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php
I am working on a php mysql connect script.
I wanted it to use functions so I can keep a track what is what, so Mysql connect got one function.
When I run it, I get first "No database selected" and when I specify it manually, it says "Access denied for # localhost".
Code
<?php
/* Mysql Data */
$MySqlUser = "root";
$MySqlPass = "**********";
$MySqlHost = "localhost";
$MySqlDataBase = "serveradmin";
/* End Mysql Data */
function MySqlConnect() {
$Connect = mysql_connect($MySqlHost, $MySqlUser, $MySqlPass);
$Database = mysql_select_db($MySqlDataBase);
if (!$Connect | !$Database) {
die("Cannot connect ".mysql_error());
}
}
MySqlConnect()
?>
So the problem, what causes this? I want the script to be nice and clean, and not sure if function() causes it.
This will fix it, but please look into PDO or mysqli
<?php
/* Mysql Data */
$MySqlUser = "root";
$MySqlPass = "**********";
$MySqlHost = "localhost";
$MySqlDataBase = "serveradmin";
/* End Mysql Data */
function MySqlConnect($MySqlUser, $MySqlPass, $MySqlHost, $MySqlDataBase ) {
$Connect = mysql_connect($MySqlHost, $MySqlUser, $MySqlPass);
$Database = mysql_select_db($MySqlDataBase);
if (!$Connect | !$Database) {
die("Cannot connect ".mysql_error());
}
return $Database;
}
$Database = MySqlConnect($MySqlUser, $MySqlPass, $MySqlHost, $MySqlDataBase );
?>