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
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'm having this problem where it says access denied, even though the login data is correct. I've tried multiple servers but it will keep saying access denied. The machine does have access to the server. I think it's my wamp that's not working.
Error:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'xml'#'localhost' (using password: YES)
Class:
class db
{
private $conn;
function __construct()
{
}
public function setupConnection()
{
if (!isset($conn)) {
$mysql_host = 'localhost';
$mysql_username = 'xml';
$mysql_password = '12345';
$mysql_db = 'xml';
try {
$conn = new PDO("mysql:host=" . $mysql_host . ";dbname=" . $mysql_db . "", $mysql_username, $mysql_password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
echo "Connectie is gefaald!: " . $e->getMessage();
}
return $conn;
}
}
}
I am trying to use connect to database, however I get this error, I am running this on php terminal.
I checked out other sources as well like this one but didn't have the answer I was looking for
Any suggestions or best practices ?
SQLSTATE[HY000] [2002] Connection refusedsomething went wrong
Db.php
<?php
error_reporting(-1);
class Db{
private $db_host;
private $db_user;
private $db_name;
private $db_pass;
public function __construct()
{
$this->db_host = "127.0.0.1";
$this->db_user = "root";
$this->db_pass = "";
$this->db_name = "eli9";
try{
$pdo = new PDO("mysql:host=127.0.0.1;dbname=eli9", $this->db_user, $this->db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "connected sucessfully \n";
}
catch(PDOexception $e){
echo $e->getMessage();
echo "something went wrong \n";
}
}
}
index.php
<?php
require_once 'Db.php';
$db = new Db();
With the help of #addie code, i was able to find the problem, and it was the following
1) i originally was not using a port number
2) i changed the port number to 8889 instead of 3306
so here is the final code for Db.php
<?php
error_reporting(-1);
class Db{
private $db_host;
private $db_user;
private $db_name;
private $db_pass;
public function __construct()
{
$this->db_host = "127.0.0.1";
$this->db_user = "root";
$this->db_pass = "root";
$this->db_name = "eli9";
try {
$db = new PDO("mysql:host=127.0.0.1;port=8889,dbname=eli9", 'root', 'root');
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
echo "connected";
}
catch (PDOException $e){
echo $e->getMessage();
}
}
}
Thank you #addie, but my pervading question is why do i need to include the port in the pdo if that is not the php manual ?
try this, works for me
$pdo = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user, $this->db_pass);
you set variable into $this->db_host = "127.0.0.1" but didn't use into connection
try this without class till you get connected and use $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); to check if there any error
also try port 8889
try {
$db = new PDO("mysql:host=127.0.0.1;port=3306,dbname=eli9", 'root', '');
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
echo "connected";
}
catch (PDOException $e){
echo $e->getMessage();
}
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 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