I'm in a situation where my Windows hosting has PHP support, but the PHP is not configured to support ODBC or MSSQL. I can't get them to change that, so I'm wondering if there's a way to connect to SQL Server through other means - maybe including some files that provide the functions that I'd need?
Leaving it up here in the hopes that it will make it easier for other people to get around this type of limitation.
Copied here for completeness:
<?php
$db = new COM("ADODB.Connection");
$dsn = "DRIVER={SQL Server}; SERVER={SERVER};UID={USER};PWD={PASS}; DATABASE={DB}";
$db->Open($dsn);
$rs = $db->Execute("SELECT * FROM table");
while (!$rs->EOF)
{
echo $rs->Fields['column']->Value."<BR>";
$rs->MoveNext();
}
?>
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 want to connect to old mysql server (Server is 4.0.20-standard) with php7. Upgrade mysql server or downgrade php is not an option.
I'm doing this: $conn = mysqli_connect($local,$user,$pwd,$db,$port);
But i'm getting this error: Warning: mysqli_connect(): Connecting to 3.22, 3.23 & 4.0 is not supported. Server is 4.0.20-standard.
Is possible do that?
The mysql native client(mysqlnd) which has been the default mysql driver since php 5.4 does not support old mysql servers, if you still want to connect to old versions of mysql servers, you should use another mysql driver libmysqlclient, which is needed to be compiled with php.
Here is the introductions of these two drivers:
https://dev.mysql.com/doc/apis-php/en/apis-php-mysqlinfo.library.choosing.html
I recommend you to build your mysqli with libmysqlclient and build your pdo with mysqlnd which is the default one and is recommended by official, so that you can connect to both versions of mysql servers.
Solution (didactic solution, do not use this on production environment):
Step 1: Install putty.exe + plink.exe in php7 machine. Step 2: I created these methods to do all the job for you/me. Example1:
$query = "SELECT * FROM user";
$rs = select($query);
if($rs)
for($index = 0; $index < count($rs);$index++){
//ALWAYS use this. Now u can use that array like a normal resultset $rs["lalala"]
$row1 = $rs[$index];
//use this like usual
echo $rs["username"];
}
Example2:
$query = "SELECT * FROM user";
$rs = select($query);
if($rs)
for($index = 0; $index < count($rs);$index++){
//ALWAYS use this. Now u can use that array like a normal resultset $rs["lalala"]
//$row1 = $rs[$index];
//now you didn't the line above...
echo $rs[$index]["username"];//you'll need something like $rs[0]["username"]
}
For update/delete/insert you can use something similar to the insert method. Example 3:
if(m_insert("insert into user (name,pwd,email) values ('kkk', 'hahaha', 'email#lala.com');")){
echo "</br>true</br>";
} else {
echo "</br>false</br>";
}
Remember1: You need to change the method variables (host, database, username, password, etc)...
Remember2: Change the mysql credentials at this line $commands[0] = 'mysql -u username -pPASSWORD...'
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 have an MS Access database file that I want to copy into a MySQL to serve up on a webpage, the problem is the database is passworded. What I want to do is upload the file to the server then either strip the password or open it using the password so I can then copy it across to MySQL.
The password is known and cannot be removed at source.
I would like to do this with PHP if possible.
This is a recurring event, at max twice a day.
Having contacted my hosting the only way to use odbc is to upgrade to dedicated hosting at 10x the price of my current hosting. Looks like this one is a no go unless I can get at the data another way.
To open it, the password should be passed along in the connection string... For PHP using odbc_connect, the syntax is available here. Since you say the password is known, this should work.
To remove it completely, you'd want to just open it in Access and save a copy without the password. I'm not sure that this can be automated easily. If you need to access the data and transfer it repeatedly, I'd say stick with the password int he connection string.
Example from the article linked to:
<?php
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
// Microsoft Access
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
// Microsoft Excel
$excelFile = realpath('C:/ExcelData.xls');
$excelDir = dirname($excelFile);
$connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');
?>
Here is the DSN - Less connection code sample :
<?php
$db_connection = new COM("ADODB.Connection");
$db_connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("../databases/database.mdb") ." ;DefaultDir=". realpath("../databases");
$db_connection->open($db_connstr);
$rs = $db_connection->execute("SELECT * FROM Table");
$rs_fld0 = $rs->Fields(0);
$rs_fld1 = $rs->Fields(1);
while (!$rs->EOF) {
print "$rs_fld0->value $rs_fld1->value\n";
$rs->MoveNext(); /* updates fields! */
}
$rs->Close();
$db_connection->Close();
?>
Can I use PHP with Oledb connection?
As far as I know database connection as provided by PHP extension are all odbc.
You can use ActiveX Data Objects (Microsoft's OLEDB ActiveX layer) in PHP-Win without any third party extension as such:
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
// Microsoft Access connection string.
$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\inetpub\wwwroot\php\mydb.mdb");
// SQL statement to build recordset.
$rs = $conn->Execute("SELECT myfield FROM mytable");
echo "<p>Below is a list of values in the MYDB.MDB database, MYABLE table, MYFIELD field.</p>";
// Display all the values in the records set
while (!$rs->EOF) {
$fv = $rs->Fields("myfield");
echo "Value: ".$fv->value."<br>\n";
$rs->MoveNext();
}
$rs->Close();
Look at the ADOdb Library for PHP extension. I've never used it, but it seems to be compatible with OLEDB providers.
maybe......
found an article on it.
found the PHP extension for it.
Don't know anything about it. Best of luck.