Essentially I need to convert the following pdo connection method to reference my mysql socket connection file - this is for my localhost environment. Connection to the localhost mysql database works fine in mysql workbench, just not sure how to reference the socket instead of the host address in PDO:
Current:
$host = 'localhost:8889';
$user = 'root';
$pass = 'root';
$db = 'mydb';
$dsn = "mysql:host=$host;dbname=$db;charset=$this->charset";
$this->pdoConnect = new \PDO($dsn, $user, $pass, $this->options);
Need to use the following socket file:
/Applications/MAMP/tmp/mysql/mysql.sock
Related
I need to connect to an Azure SQL DB from an external PHP app on shared cPanel hosting.
My host has enabled mssql_connect but wont enable MS's recommended sqlsrv_connect in the shared environment.
I have whitelisted the webserver IP in Azure's Portal, and whitelisted the DB address in cPanel.
The test script seems to try to connect, but cant. No error information is provided: http://app.hivve.com.au/api/
PHP is:
$db_server = "cjweb.database.windows.net:1433"; // update me
$db_username = "username";
$db_password = "password";
$conn = mssql_connect($db_server, $db_username, $db_password);
print_r(mssql_get_last_message());
Has anyone else been down this road? My host wont provide anymore assistance so Im stuck.
Could you please try to connect as shown below? Please look at the user name.
Add [aftermath] on the ~/.freetds.conf:
[aftermath]
database = mydatabase
host = cjweb.database.windows.net
port = 1433
tds version = 8.0
$myServer = "aftermath"
$myUser = cjweb#cjweb
$myPass = your_password
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
What I found in Azure SQL document Quickstart: Use PHP to query an Azure SQL database that maybe you have lost the "UID" agrument.
<?php
$serverName = "your_server.database.windows.net"; // update me
$connectionOptions = array(
"Database" => "your_database", // update me
"Uid" => "your_username", // update me
"PWD" => "your_password" // update me
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
?>
You can first test Alberto Morillo's code.
Hope this helps.
I`m working on a java swing app. so that i have created a database called c_app in MySQL server workbench. what i need is to fetch data into a HTML page using PHP.
$username = "root";
$password = "";
$hostname = "localhost:3307";
$db = "c_app";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password, $db)
or die("Unable to connect to MySQL");
This code segment gives me Warning: mysqli_connect(): (HY000/1049): Unknown database 'c_app' in C:\xampp\htdocs\C_AppWeb\linkpage.php on line 7
Unable to connect to MySQL
What is the problem here? do i need phpmyadmin database rather than MySQl database? can anyone help me?
First one, both phpmyadmin and workbench are not database. They are tools to work with database.
I see you use port 3307 in your code, so let you try:
$username = "root";
$password = "";
$hostname = "localhost";
$port = 3307;
$db = "c_app";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password, $db, $port)
or die("Unable to connect to MySQL");
Try to connect to remote mysql via ssh2 and php.
$ssh_server='';
$ssh_port='';
$ssh_user='';
$ssh_password='';
$ssh_connection=ssh2_connect($ssh_server, $ssh_port);
$ssh_auth=ssh2_auth_password($ssh_connection, $ssh_user, $ssh_password);
$ssh_tunnel = ssh2_tunnel($ssh_connection, $ssh_server, $ssh_port);
$db_hostname = 'localhost';
$db_database = '';
$db_username = '';
$db_password = '';
$db_port = '3306';
$connection_mysql = new mysqli($db_hostname, $db_username, $db_password, $db_database, $db_port);
Tunnel is ok, but I see, that mysql use my local mysql DB, not remote via SSH. So, how can i use mysql via SSH?
OS windows with WAMP on it.
create private/public key and setup key authentication with ssh(aka passwordless authentication).
then run this code in your php script
shell_exec("ssh -f -L 3307:localhost:3306 myuser#10.0.0.1 sleep 10 >> logfile");
then create database connection like:
$pdo = new PDO("mysql:host=$servername;dbname=$dbname;port=3307", $username, $password);
$ssh_connection = ssh2_connect($ssh_server, $ssh_port);
ssh2_auth_password($ssh_connection, $ssh_user, $ssh_password);
$query="SET CHARACTER SET utf8; use ${db_database}; ${query};";
$query=str_replace('"', '\'', stripslashes($query));
$ssh_query="ssh -L 3307:${ssh_server}:${ssh_port}; echo \"${query}\" | mysql -u ${db_username} -h ${db_hostname} --password=${db_password}";
$result=ssh2_exec($ssh_connection, $ssh_query);
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 typically connect using the following:
$db_host = "localhost";
$db_username = "bill";
$db_pass = "mypassword";
$db_name = "theDB";
mysql_connect("$db_host","$db_username","$db_pass", TRUE) or die(mysql_error());
mysql_select_db("$db_name") or die("no database by that name");
I tried changing the host to the RDS endpoint below with no luck:
$db_host = "mysql7.1234567890123.us-west-2.rds.amazonaws.com";
-The security group has inbound rules that allow my IP.
-I've tried adding "10.0.0.0/8" to the security group from Connecting From PHP Server to MySQL Server on Amazon AWS
Other info: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_PHP_legacy.rds.html
There are couple of things and use PDO or mysqli to connect. If you've made sure that you can connect to RDS by setting the right security group, then you've got the main issues squared away.
Connect to Amazon RDS with this in PHP:
$dbhost = $_SERVER['RDS_HOSTNAME'];
$dbport = $_SERVER['RDS_PORT'];
$dbname = $_SERVER['RDS_DB_NAME'];
$dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname}";
$username = $_SERVER['RDS_USERNAME'];
$password = $_SERVER['RDS_PASSWORD'];
$dbh = new PDO($dsn, $username, $password);
$link = mysqli_connect($_SERVER['RDS_HOSTNAME'], $_SERVER['RDS_USERNAME'], $_SERVER['RDS_PASSWORD'], $_SERVER['RDS_DB_NAME'], $_SERVER['RDS_PORT']);
Example 1. Example using PDO to connect to an RDS database
$dsn = 'mysql:host=mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com;port=3306;dbname=mydb';
$username = 'sa';
$password = 'mypassword';
$dbh = new PDO($dsn, $username, $password);
Example 2. Example using mysqli_connect() to connect to an RDS database
$link = mysqli_connect('mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com', 'sa', 'mypassword', 'mydb', 3306);
if nothing works, launch a mysql client like navicat or mysql workbench (free) and punch in all the necessary fields and try to connect and see if it loads the db.
This ended up working for me. Thanks for you help!
$db_host = 'instance2.123456789.us-west-2.rds.amazonaws.com:3306'; //RDS Endpoint...
$db_username = 'myusername';
$db_pass = 'mypassword';
$db_name = 'myDB';
mysql_connect("$db_host","$db_username","$db_pass", TRUE) or die(mysql_error());
mysql_select_db("$db_name") or die("no database by that name");