PHP wont connect to Azure SQL DB with mssql_connect - php

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.

Related

How can I convert this pdo connection to use socket | PHP

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

Failed login to MSSQL with PHP

I am trying to log into a local SQL database, but I keep getting an error about 'Login Failed'. This is the first time that I have tried to connect to a MS SQL server... normally I use MySQL. So, I am not even sure if I am doing this correctly.
Here is my connection code:
function dbConnection()
{
$host = '127.0.0.1';
$db = 'my_db';
$user = 'dbuser';
$pass = "my_pw";
$serverName = "tcp:$host,1443";
$connectionOptions = array(
"Database"=>$db,
"Uid"=>$user,
"PWD"=>$pass
);
$connection = sqlsrv_connect($serverName, $connectionOptions);
if (!$connection) print_r(sqlsrv_errors());
return $connection;
}
print_r(dbConnection());
Everytime I try this, I get:
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for
user 'dbuser'.
The username and password are correct, and the permission are set correctly. The port is correct, too. I can log into SSMS just fine, but not through PHP.
Any ideas?
Assuming that your mssql server instance is say sqlexpress..
Please use
$serverName = "serverName\\sqlexpress";
or if the port is 1443, then
$serverName = "serverName\\sqlexpress, 1443"

Database connection Unknown error in PHP with MySQL server workbench not in phpmyadmin

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");

Connect to MySQL on Amazon AWS RDS

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");

c9.io php pdo connect to mysql

I am attempting to connect to a mysql database using the c9.io development environment. I have followed their documentation and have seen multiple links, 1, 2 and 3.
I verified the mysql service is running. I also verified the PDO extension was installed via phpinfo(). Here is my current code:
$ip = getenv("REMOTE_ADDR");
$port = '3306';
$user = "username";
$db = "c9";
try{
$con = new PDO("mysql:host=$ip;port=$port;dbname=$db;charset=utf8",$user,"");
}
catch(Exception $e){
echo $e->getMessage();
}
I get the error Can't connect to MySQL server on '10.240.x.x' (111)
If i try localhost as host, I get the error Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I also followed a comment from the second link above: echo $IP in the terminal which returns 0.0.0.0
Any assistance appreciated.
You were on the right track. On https://docs.c9.io/setting_up_mysql.html it says use $IP for host. You can use getenv("IP") instead or use its value: 0.0.0.0. That should work.
Please try something like:
$dbname = 'c9';
$ip = getenv('IP');
$user = getenv('C9_USER');
mysql_connect($ip, $user, '') or die('Could not connect to mysql');

Categories