I have a linux server that I'm trying to use php adodb to connect to a MSSQL server.
include('adodb5/adodb.inc.php');
$conn =& ADONewConnection('odbc_mssql');
$dsn = "Driver={SQL Server};Server=MSSERVER;Database=Northwind;";
$conn->Connect($dsn,'sa','password')or die("Unable to connect to server");
I've install mssql through yum etc and I know the server can connect to it as I've tried the following:
$db = #mssql_connect("MSSERVER","sa","password") or die("Unable to connect to server");
mssql_select_db("Northwind");
// Do a simple query, select the version of
// MSSQL and print it.
$version = mssql_query('SELECT ##VERSION');
$row = mssql_fetch_array($version);
echo $row[0];
// Clean up
mssql_free_result($version);
Any ideas why my adodb wont connect, or any examples on how I can connect would be much appreciated.
I've solved this by looking at this forum: http://ourdatasolution.com/support/discussions.html?topic=4200.0
The correct code is:
<?php
include("adodb5/adodb.inc.php");
//create an instance of the ADO connection object
$conn =&ADONewConnection ('mssql');
//define connection string, specify database driver
$conn->Connect('xxx.xxx.x.xxx:1400', 'user', 'password', 'DbName');
//declare the SQL statement that will query the database
$query = "select * from table";
$rs = $conn->execute($query);
//execute the SQL statement and return records
$arr = $rs->GetArray();
print_r($arr);
?>
Hope that helps somebody else.
With php 5.3 of above the php_mssql modul is no longer supported for windows.
A Solution is to download the MicroSoft PHP Driver from http://www.microsoft.com/en-us/download/details.aspx?id=20098.
This installer will extract the modul dll files to you php extension directory.
Include the correct version in you php ini (e.g. like this for php 5.3 ThreadSafe):
extension=php_sqlsrv_53_ts.dll
After this you can use adboDb again, but you have to use mssqlnative as adodbtype.
And the connection with ip and port didnt work for me, but ipaddress\\SERVERNAME worked (see examplecode)
<?php include("adodb5/adodb.inc.php");
//create an instance of the ADO connection object
$conn =&ADONewConnection ('mssqlnative');
//define connection string, specify database driver
// $conn->Connect('xxx.xxx.x.xxx:1400', 'user', 'password', 'DbName');
$conn->Connect('xxx.xxx.x.xxx\\SERVERNAME', 'user', 'password', 'DbName');
//declare the SQL statement that will query the database
$query = "select * from table";
$rs = $conn->execute($query);
//execute the SQL statement and return records
$arr = $rs->GetArray();
print_r($arr); ?>
For PHP 7.4 you can download the drivers here:
https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver15
Copy the files to the ext directory of your php installation.
In the php.ini file then add the extensions as follows:
extension=php_pdo_sqlsrv_74_ts_x64
extension=php_sqlsrv_74_ts_x64
The extension also requires the ODBC driver for SQL Server to be installed:
https://learn.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver15
Related
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>";
}
I read more than once that is better to use mysqli and than mysql.
I would like to know:
I'm using WAMP for my local server and some share server for my online sites. what do i need to check on php.ini in order to be sure that mysqli will works?
below is my db connection and query code. how would it looks like using mysqli?
$con = #mysql_connect ("localhost", "username", "pass");
if(!$con)
exit("Unable to connect to the Database server");
$db2 = #mysql_select_db("DB_NAME");
if(!$db2)
exit("Unable to connect to the Database");
$query = mysql_query("SELECT somme_filed FROM some_table");
while ($index= mysql_fetch_array($query ))
{
....
}
most of my site was written in mysql. Do I really need to change all the code to mysqli? Is it really so important?
1.As long as you have only one instance of php installed there should not be any problem but if you have more than one instance you may need to point it manually to the version you wish to use. How do I activate mysqli with wampserver?
2.You can use mysqli to connect by doing something like this
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
3.MYSQL is no longer under development and has been considered deprecated.
Why shouldn't I use MYSQL functions
I am porting a site running PHP with an MS Access DB on a windows machine to a Mac with an SQLite DB.
the original PHP script uses the following code to connect to the database:
$db = 'S:\~myhome\mydata.mdb';
$conn = new COM('ADODB.Connection');
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db");
What would be the SQLite equivalent?
Edit:
I tried
$db = 'sqlite:'.__DIR__.'/mydata.sqlite';
$conn = new PDO($db) or die("cannot open the database");
but it didn't work
Like Python, PHP has a built in SQLite library. Current versions support SQLite3. First, uncomment out the php_sqlite extension in the .ini file. Then, simply, call a new object:
<php
$conn = new SQLite3($db);
$results = $conn->query('SELECT bar FROM foo');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
Of course as suggested you can use PDO or mysqli database connections.
Since you have to work on it anyway, I suggest to use PDO. This is a standard PHP library with drivers for several database types.
For a quick start see http://www.phptherightway.com/#databases, there's a short example about SQLite as well.
after much searching I found the answer:
include '/usr/share/php/adodb/adodb.inc.php';
$path = urlencode(__DIR__.'/mydata');
$dsn = "sqlite://$path/?persist"; # persist is optional
$conn = ADONewConnection($dsn);
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
I have PHP sample code which will fetch the data from the MYSQL database through ODBC driver on Linux(CentOS) machine.
I have created DSN and same able to connect through following command
isql -v
But when i try to same DSN through PHP code i am getting "No tuples available at this result index" due to which unable to read the data from database through PHP APACHE configuration.
If anyone provides the solution,It will more helpful for me to proceed further.
Below are my sample code and other details,Please correct if anything wrong on below configuration details-
Below is sample PHP code:
<?php
$conn = odbc_connect("DSN", "username", "password");
$sql = 'select * from tablename';
$rs = odbc_exec($conn,$sql);
echo "<table><tr>";
echo "<th>User Name</th></tr>";
while(odbc_fetch_row($rs)) {
$user = odbc_result($rs,"fieldname");
echo "<tr><td>$user</td></tr>";
}
odbc_close($conn);
echo "</table>";
?>
Below is odbc.ini file:
[DSN]
Description = MySQL ODBC Database
DRIVER = MySQL
TraceFile = /tmp/odbcerr.log
SERVER = 127.0.0.1
PORT = 3306
USER = username
PASSWORD = password
DATABASE = database
OPTION = 3
SOCKET = /var/lib/mysql/mysql.sock
Below is odbcinst.ini file:
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc5.so
Setup = /usr/lib/libodbcmyS.so
FileUsage = 1
UsageCount = 3
The comments in the documentation for odbc_connect suggest that this can be caused by using an incorrect cursor type. Try each of the three possible values as the fourth argument: SQL_CUR_USE_IF_NEEDED, SQL_CUR_USE_ODBC, or SQL_CUR_USE_DRIVER.
I am very curious as to why you're using ODBC to connect to MySQL. PHP has many better ways to read from MySQL, including the excellent PDO class (which itself can connect via ODBC if needed).