Connecting php to MS SQL server 2008 - php

I need a connection script of php and MS SQL:
in trying the following but it doesn't work
$server = 'MVEKELDG156\SQLEXPRESS'; $username = 'EDUC.PWV.GOV.ZA\Mveke.L';
$password = 'password#'; $database = 'SAMS_EMIS_Warehouses';
if(!mysql_connect($server, $username, $password)) { exit('Error: could not
establish database connection'); } if(!mysql_select_db($database)) {
exit('Error: could not select the database'); }

The MSSQL extension is enabled by adding extension=php_mssql.dll to php.ini.
To get these functions to work, you have to compile PHP with --with-mssql[=DIR] , where DIR is the FreeTDS install prefix. And FreeTDS should be compiled using --enable-msdblib .
More References :
Installation
How to use PHP to connect to sql server
Connecting to an SQL Server Database with PHP

Check this
$myServer = "localhost";
$myUser = "your_name";
$myPass = "your_password";
$myDB = "SAMS_EMIS_Warehouses";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB
Source How to connect to MS SQL Server database

Related

PHP wont connect to Azure SQL DB with mssql_connect

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.

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

how to mysql database connection in php for sqlyog

I have a php file to import .csv file into database. For that i am using connection strings like below :
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'demo-eams';
$db_connect = mysql_connect($db_host, $db_user, $db_pass) or die ("Could not connect to MySQL");
$db_select = mysql_select_db($db_name) or die ("Could not connect to database");
i am using sqlyog for access mysql database.
My problem is whenever i run my coding it showing the connection error :
Could not connect to Mysql.
How to solve this?
Check following:
Check if your mysql is up and running
Validate mysql port (if its not default then add port in host name)

How to set Max Pool Size in database connection string using PHP and SQL Server?

I have a connection string like this:
$cs = "server=localhost;database=Test;uid=admin;pwd=123456";
normaly, I split the string then connect to database by using this:
// $myserver = "localhost"
// $myuser = "admin"
// $mypwd = "123456"
// $mydb = "Test"
//connect to server
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select the database
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
But today, I need to connect to the database with Max Pool Size in connection string.
$cs = "server=localhost;database=Test;uid=admin;pwd=123456;Max Pool Size=100";
How can I add "Max Pool Size=100" option while connect to the database? What if there are more option?

PHP PDO dblib not working

I am trying to connect to MS SQL database which is hosted on a different set of servers. I can connect the old way.
$myServer = "server name";
$myUser = "username";
$myPass = "pword";
$myDB = "dbname";
if (is_callable('mssql_connect')) {
$link = mssql_connect($myServer, $myUser, $myPass);
if (!$link) {
die('connection failed');
}
} else {
echo 'mssql_connect() is not supported on this environment';
}
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass);
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
That works just fine to connect to the ms sql server. When I try with PDO dblib I get the following error
"SQLSTATE[01002] Adaptive Server connection failed (severity 9)"
Here is the code I am using for that and I have checked and pdo is installed with dblib as an option to use. Credentials are all exactly the same.
$myServer = "server name";
$myUser = "username";
$myPass = "pword";
$myDB = "dbname";
try {
# MS SQL Server and Sybase with PDO_DBLIB
$DBH = new PDO("dblib:host=$myServer;dbname=$myDB, $myUser, $myPass");
}
catch(PDOException $e) {
echo $e->getMessage();
}
Any help would be appreciated as I am planning to create a new application in php to connect to this MS SQL db but in the future plan to migrate db over to Mysql once all of the old classic asp's are rebuilt into php.
The PDO connection syntax is supposed to be like this,
$DBH = new PDO("dblib:host=$myServer;dbname=$myDB", $myUser, $myPass); //wrongly placed quotes
Manual

Categories