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');
Related
I have setup an instance and now I want to connect my website with database. What would be the hostname If I want to connect to phpmyadmin. Will hostname be localhost?.I am using a following code:-
class Database{
// specify your own database credentials
private $host = "localhost";
private $db_name = "PHPLearning";
private $username = "root";
private $password = "";
public $conn;
// get the database connection
public function getConnection(){
$this->conn = null;
try{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->exec("set names utf8");
}catch(PDOException $exception){
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
your code is correct. but it not work then change host from localhost to 127.0.0.1
i wrote a php script which should connect me with my database. I also uploaded the files in the public_html section, but it always throws this error :
Warning: mysqli::mysqli(): (HY000/2002): Connection refused in /Applications/XAMPP/xamppfiles/htdocs/FoodHelperSwift/db/public_html.php on line 24
Here is the line : $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
This is my code :
public_html
var $conn = null;
var $result = null;
public $dbhost = null;
public $dbname = null;
public $dbuser = null;
public $dbpass = null;
function __construct($dbhost, $dbuser, $dbpassword, $dbname) {
$this->dbhost = $dbhost;
$this->dbuser = $dbuser;
$this->dbpass = $dbpassword;
$this->dbname = $dbname;
}
public function openConnection()
{
$this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
echo "YESSSS";
echo $this->dbhost;
echo $this->dbuser;
echo $this->dbpass;
echo $this->dbname;
if(mysqli_connect_errno())
{
throw new Exception("Could not connect with database");
$this->conn->set_charset("utf8");
}
}
public function closeConnection()
{
if ($this->conn != null)
{
$this->conn->close();
}
}
}
and
register user
:
require("../db/public_html.php");
$dbhost = "127.0.0.1";
$dbname = "xxxxxxxx";
$dbuser = "xxxxxxxxx";
$dbpassword = "xxxxxxx";
$returnValue = array();
if(empty($_REQUEST["userEmail"]) || empty($_REQUEST["userPassword"])
|| empty($_REQUEST["userFirstName"])
|| empty($_REQUEST["userLastName"]))
{
$returnValue["status"]="400";
$returnValue["message"]="Missing required information";
echo json_encode($returnValue);
return;
}
$userEmail = htmlentities($_REQUEST["userEmail"]);
$userPassword = htmlentities($_REQUEST["userPassword"]);
$userFirstName = htmlentities($_REQUEST["userFirstName"]);
$userLastName = htmlentities($_REQUEST["userLastName"]);
$salt = openssl_random_pseudo_bytes(16);
$secured_password = sha1($userPassword . $salt);
$dao = new MySQLDAO($dbhost, $dbuser, $dbpassword, $dbname);
$dao->openConnection();
The password etc. is right and i also tried localhost, but then i got an error that he cant find the file maybe you could help me.
Most likely you're not following steps provided by your website hosting provider.
Third party hosting solutions usually require you setup your remote IP
From the error message you specified it looks like you're trying to do this from your own home XAMP web service.
First - the basics
:
localhost - won't work from home - because that's going to look for your own MySQL database not the hosted db
Second - logging in from home (or remotely)
read the docs (always a good idea) Remote access to Bluehost MySQL
Use the following configuration settings for connecting to your database
Host name = (use the server IP address)
Database name = (cpanelUsername_databaseName)
Database username = (cpanelUsername_databaseUsername)
Database password = (the password you entered for that database user)
MySQL Connection Port = 3306
TCP or UDP, either is fine.
Allowing a Remote Server to Access Your Database
Before connecting to MySQL from another computer, the connecting computer must be enabled as an Access Host.
Log into cPanel and click the Remote MySQL icon, under Databases.
Type in the connecting IP address, and click the Add Host button.
Note: You can find and add your IP address directly from this tool. Look for Your IP is: 123.123.12.123 [Add]. Clicking the [Add] link will input your IP into the field box below.
Click Add, and you should now be able to connect remotely to your database.
To troubleshoot this, strip your code down to the basics. Make yourself a little testMyDb.php file, containing only the minimal stuff. For example:
$dbhost = "127.0.0.1";
$dbname = "xxxxxxxx";
$dbuser = "xxxxxxxxx";
$dbpassword = "xxxxxxx";
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
Once you have that working, you can proceed to debug your php class and make sure you are setting things up correctly.
Your file named public_html.php contains some code that's part of a php class implementation (for example __construct()), but I don't see a class ClassName { line to set up a class definition. It's possible you've copied some fragments of code from somewhere without getting it all.
If your simple test DOESN'T work, check with bluehost's tech support krewe. You may need some special credentials or database name to connect to MySQL from one of their Windows hosts.
If you're using the MySQL server on a bluehost machine, and trying to connect to it from your local machine, that will not work (especially not with 127.0.0.1). You'll need to configure bluehost to allow remote MySQL connections, and you'll have to use the actual MySQL hostname.
I have Lamp server in my Ubuntu. I worked on Api's using slim framework and with mysql database and its working fine. My problem is I cant connect my api to MSSQL.
I already install freetds in ubuntu
I also include this in freetds.conf
[myserver]
host = myhost
port = myport
here's my connection:
function getConnection() {
$dbhost="myserver.database.windows.net";
$dbuser="user";
$dbpass="mypass";
$dbname="myDB";
$dbh = new PDO("dblib:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
here's my api in slim:
$app = new Slim\App();
$app->get('/clients', 'getClients');
$app->run();
function getClients() {
$sql = "select * FROM mytable";
try {
$db = getConnection();
$stmt = $db->query($sql);
$clients = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo '{"client": ' . json_encode($clients) . '}';
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
I think you are missing something....
I think you need to let PDO know what port
http://php.net/manual/en/ref.pdo-dblib.php
$hostname = "myhost";
$port = 10060;
$dbname = "tempdb";
$username = "dbuser";
$pw = "password";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
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;
}
}
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