Trying to connect with sql server database - php

I'm trying to connect sql server with php, i'm trying to get info from the database..
Now, here is what i got:
try {
$user = '';
$pass = '';
$objDb = new PDO('mysql:host=192.168.10.250;dbname=WEB_POROSIA',
'$user', '$pass');
$objDb->exec('SET CHARACTER SET utf8');
$sql = "SELECT *
FROM 'WEB_POROSIA'
";
$statement = $objDb->query($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo $e->getMessage();
}
I receive this error:
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
This is my first attempt to connect php with sql server, and i don't know what this output means, i mean i don't know what might cause it!
I'm using xampp.
Thanks

It appears that you are trying to connect using the wrong driver.
For Sql Server you might want to use PDO ODBC driver not the mysql method you are trying to use.

You need to use a different DBO driver.
Check out other ODBC drivers, i.e PDO.

The code you have written tries to connect to a MySQL database rather than a MSSQL one.
You'll want to do something like this:
$db_connection = new PDO("dblib:dbname=$db_name;host=$host", $username, $password);
You can find more information here: http://grover.open2space.com/content/use-php-and-pdo-connect-ms-sql-server

Related

How to connect to mysql database with php?

I'm using Jetbrains and Mysql to work on this practical project, but when I connect to the mysql
database it gives me the following error:
C:\wamp64\bin\php\php5.6.40\php.exe C:\wamp64\www\Social_Network\Includes\connection.php
Connection failed: SQLSTATE[HY000] [1049] Unknown database 'social_network'
Process finished with exit code 0
I made sure several times that the database name is the same name and there are
no spelling errors at all (I copy pasted it from the database)
Here's my code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=social_network", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
//?>
Welll, there's little that can be done about it. MySQL thinks that the database does not exist.
is the server the correct one?
is the case sensitivity set correctly? "Social_Network" and "social_network" might be considered different.
can you access the database with those parameters using a different tool (e.g. HeidiSQL, SQLYog, SQLterm, in a pinch even phpMyAdmin)?
Actually, JetBrains PHPStorm has a SQL terminal utility that can diagnose the connection. You may want to use it (once it knows what database you're connecting to, it will also warn you of several possible errors such as using the wrong table name or column name).

How can I connect from a website to a Microsoft SQL Database using PHP

Ive worked with the typical PHPMyAdmin interface until now when it comes to databases.
Now Ive been handed a .bak file and pointed to "Microsoft SQL Server Managment Studio" and told to use that.
My problem is that I dont know how to do the most basic things. For instance when I want to connect to a PHPMyAdmin Database I know that I need to define the host, the user, the password and the database and throw that information into a mysqli_connect() and thats that.
So my question is:
How do I connect to my database that I have in my SQL Server Managment Studio? The same way I did before? And if so where do I find the needed information like host, user, password?
I would install the SQLSRV PDO extension. Then use a similar methodology to access the database
$database = "";
$server = "";
$conn = new PDO( "sqlsrv:server=$server ; Database = $database", DB_USER, DB_PASSWORD);
$sql = "SELECT * FROM Table WHERE MemberID =:MemberID";
$iMemberID = 5;
$st = $conn->prepare($sql);
//named placeholders within the execute() as an array.
$st->execute(array(':MemberID'=>$iMemberID));
//OR using bind param..
$st->bindParam(':MemberID', $iMemberID);
while ($row = $st->fetch()){
echo "<pre>";
var_dump($row);
echo "</pre>";
}

How to connect to sql database through php/pdo that has been exported from mysql/phpmyadmin as mydatabase.sql?

I have searched on many different sites and cannot find an answer that specifically answers this question:
I have WAMP Server installed, which installed phpmyadmin as well as MySQL. I can easily connect to the database through php/pdo by using localhost as my hostname.
But my problem is I am trying to connect to a database that I have exported from phpmyadmin as "mydatabase.sql". So now lets say I placed this file on my pc at "C:\Users\Username\Desktop\MyFolder\mydatabase.sql". How would I connect to this database (if this is possible)?
The reason is that I cannot install WAMP Server on all the pcs that would use this program. so would like to be able to connect to the database without having to install any type of servers, etc...
My php at the moment is:
<?php
$dsn='mysql:C:\Users\Username\Desktop\MyFolder\mydatabase.sql';
$username='username';
$password='pass';
try
{
$dbh = new PDO("$dsn",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected to Database<br/>';
$sql = "SELECT * FROM users";
foreach ($dbh->query($sql) as $row)
{
echo $row["ID"] ." - ". $row["Name"] ."<br/>";
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Also, if this is possible, would username and password be necessary to connect to an "off server database"?
If I try:
$dsn='sqlite:C:\Users\Optique\Desktop\optique.sql';
I get:
"Connected to Database
SQLSTATE[HY000]: General error: 26 file is encrypted or is not a database"
But I think that is because SQLite uses a different database format than MySQL (stand to be corrected). So what I need is something similar to the SQLite driver for MySQL.
Thanks in advance!
You cannot find an answer simply because it doesn't exist: it's impossible to connect to an SQL dump through PDO, and it makes no sense anyway: a dump is not a database but simply a collection of INSERT queries. Unlike sqlite, for mysql you need a server to connect with.
To be able to connect, you should import that dump into mysql database first and then connect to that database with PDO.

Linking MS Access and database server without installing any additional drivers

on our company-website, we have an html-form, where an e-mail is generated via php for consulting reasons - which works fine. My superiors want me to save the information of the html-form into an MS Access-database.
I couldn't come up with a solution that directly writes from php into MS Access - so I convinced them to go with MySQL via phpMyAdmin - linking the tables via ODBC in Access.
The connection between php and MySQL was easy, and the connection between MS Access and MySQL would've also been:
To do that, an ODBC-MySQL-Provider is required for each Client to open the Database, which the IT-department doesn't want to install.
We also use an MS SQL Server on an Windows Server - I could also connect php to SQL Server - but in this case we need to install the ODBC SQL Server Provider when linking the tables or the SQL Server Native Clients if we go with Ado. - IT guys:
no installation of providers on all clients.
My question is:
Is there any possible solution for accessing MySQL/SQL Server
from MS Access without the installation of ODBC-Providers at all?
I have really high pressure on this and can't find anything that could satisfy user-needs and expectations.
Thanks in advance
Baris
Edit: the working php/MySQL-Code - based on mysqli
$servername = "----------.de";
$username = "-------";
$password = "--------";
$dbName = "---------";
$port = "-------";
$con= mysqli_connect($servername, $username, $password, $dbName, $port );
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
The following connection-string works for connecting to a server-side ms sqlserver from MS Access with the integrated SQLSRV32.dll-provider. Thanks to GordThompson
cs = "Driver={SQL Server};"
cs = cs & "Server=ip-adress,port-number;"
cs = cs & "Database=database-name;"
con.Open cs, "username", "password"
Since there is a possiblity to connect to the SQL Server without additional provider, I am going for the PDO/SQLServer-combination, and loading the data based on the connection-string above.
Since the overriding concern is to avoid installing additional drivers on the client machines and you have a Microsoft SQL Server available to you then you can
Have your PHP script write the required information to the SQL Server, and then
Create an Access process that retrieves the information from the SQL Server using the "SQL Server" ODBC driver that is installed with every copy of Windows.
First and foremost, PHP can absolutely connect to an MS Access database (specifically the Jet/ACE SQL Engine). In modern day computing, practically all general purpose languages (C#, Java, PHP, Perl, Python, R, VB) can connect to all popular relational database management systems (RDMS) -both file level (SQLite, MS Access) and server level (SQL Server, MySQL, Postgre, Oracle, DB2), each using some type of customized or generalized library.
With PHP's PDO, you can practically interchange the connected backend database. So however you connect to MySQL, you can model such a connection with other databases. The web popularity of PHP/MySQL make it seem they are married to each other but they are not! Below are examples using DSN, Driver, and even PHP's own API, sqlsrv.
MS ACCESS
# USING DSN
$database="C:\Path\To\Database\File.accdb";
try {
$dbh = new PDO("odbc:DSN=MS Access Database;DBq=$database;");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM tablename";
$STH = $dbh->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
# close the connection
$dbh = null;
# USING DRIVER (NON-DSN)
$database="C:\Path\To\Database\File.accdb";
try {
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBq=$database;");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM tablename";
$STH = $dbh->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
# close the connection
$dbh = null;
SQL SERVER
# WITH ODBC DRIVER PDO
try {
$dbh = new PDO("odbc:Driver={SQL Server};Server=$server;database=$database",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM dbo.tablename";
$STH = $dbh->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
# close the connection
$dbh = null;
# WITH SQLSRV PDO
try {
$dbh = new PDO("sqlsrv:server=$server;database=$database",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM dbo.tablename";
$STH = $dbh->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
# close the connection
$dbh = null;
Extra resource for other language connections:
https://github.com/ParfaitG/DATABASE_CONNECTIONS

connecting to SQL Server via PHP is failing

I have a script that creates 2 connections to the database using PDO.
The first connection is made to a SQL Server and the second one is to MySQL.
The script was working fine until yesterday. I can't think of anything that could have changed. but the script now is failing
This is the exception that I am getting when trying to connect to SQL Server
SQLSTATE[IMSSP]: Invalid value 1 specified for option PDO::SQLSRV_ATTR_QUERY_TIMEOUT.
I am running PHP 6.6 on Apache 2.4.12
I reviewed the script and the connection should be working with no issues.
This is my connection string
$connString = 'sqlsrv:Server=MyIP,1433;Database=MyDBname';
$pdo_opt[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$pdo_opt[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;
$pdo_opt[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8';
$pdo_opt[PDO::MYSQL_ATTR_LOCAL_INFILE] = true;
$pdo_opt[PDO::SQLSRV_ATTR_ENCODING] = PDO::SQLSRV_ENCODING_UTF8;
try {
$pdo = new PDO($connString, $username, $password, $pdo_opt);
} catch(Exception $e){
exit($e->getMessage());
}
How can I correct this issue? or where should I start to investigate this issue?
I figured it out. I fixed it by adding this line of code
$pdo_opt[PDO::SQLSRV_ATTR_QUERY_TIMEOUT] = 30;

Categories