I have a VB application using a Microsoft SQLServer Database File on my computer. And I want to access the information from that database online using PHP. I already installed Apache and PHP on my computer.
The problem is I don't know how to connect my PHP to the database on my VB application.
First of all, take a look at this link concerning PHP's MS SQL (Microsoft SQL) library.
You need to use the MS SQL functions to connect to the database and retrieve the relevant information you require.
I'm not an expert with MS SQL but you'll need code similar to the following:
$server = "EDDYSPC\SQLEXPRESS"; // Server address
$user = "root";
$pass = "";
$db = "MyDatabase";
$msSQL = mssql_connect($serverm $user, $root);
if (!$msSQL) { // If there was an error connecting to the database
die("An error occurred.");
}
mssql_select_db($db); // Select the datbase to use
$query = mssql_query("SELECT name FROM users WHERE name = 'Eddy'");
while ($row = mssql_fetch_assoc($query)) { // Loop through each returned row
print($row['name'] . "<br />\n");
}
Hopefully this helps.
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>";
}
Perhaps this is the connection string to connect to an sql server
<?php
$myServer = "localhost";
$myUser = "your_name";
$myPass = "your_password";
$myDB = "examples";
?>
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
but i do not know what to input in the $myServer, $myUser and $myPass
if I wanna use windows authentication
This is my Server details kindly help me please
Microsoft SQL Enterprise Manager
Microsoft Corporation
Version: 8.0
If you have PHP 5.3 or later, you can no longer use the ms_sql extension, according to the PHP documentation page. I'm not sure of the extent of that, but you may want to consider using PDO instead. It allows for a generic way to access various database types, including MSSQL. To connect to a MSSQL database using PDO, you first need SQLSRV, the driver, which you can download from Microsoft here. To connect, you would use the following:
$handle = new PDO("sqlsrv:Server=$server;Database=$database", $username, $password);
Above, and to answer the main part of your question, $server is probably localhost, and your username and password can be accessed and changed from within the Microsoft SQL Server access panel. I believe you can use "sa" for $username and "" for $password. For $database, just put in the name of the database you want to connect to.
To query:
$queryRef = $handle->query($query);
To read results, declaring $results as a two-dimensional associative array, the first being the result number, the second being the column name:
$results = $queryRef->fetchAll(PDO::FETCH_ASSOC);
So $results[3]['id'] would be the value of the column 'id' of the third result.
You can find more examples on the PHP documentation page for PDO here. Much of this was taken from my previous answer here.
Your server is usually localhost, and your username and password could be changed and viewed by following this tutorial:
http://blog.sqlauthority.com/2007/12/29/sql-server-change-password-of-sa-login-using-management-studio/
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 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();
?>
How we can convert a ms-access database to mysql database by php…
or
How we can access a ms-access database by php..
For accessing a MS Access database on Windows through ODBC, see here.
How we can convert a ms-access
database to mysql database by php
You can export via an ODBC connector or (if you don't have too many tables) you can export your data to a text file and then import it into MySQL (after creating the tables manually) via LOAD DATA. Right-click on the tables and choose Export for available options.
For more detailed info on migrating from MS Access to MySQL, check out this great article from the MySQL Dev Team:
http://dev.mysql.com/doc/mysql/en/LOAD_DATA.html
How we can access a ms-access database
by php
You can do this easily right through PDO.
You can use this simple code to connect to Access database. I tried this code in PHP,working on Windows XP with XAMPP's Apache server, and using Access 2007 file as a database. Just create your access file and try this :
First go to Start menu > Control Panel > Administrative Tools > Data Sources(ODBC) > System DSN > Add.. > Microsoft Access Driver(.mdb,.accdb) and show your access file. Give name to the connection.
Then write in your *.php file this code:
`
<?php
$host= "host_name";
$user= "user_name";
$pass= "password";
$db_connect=odbc_connect($host,$user,$pass); //connect to access file as database
if (!$db) //In case if you didn't connect , you'll get this error message
{
echo "Can't connect";
exit;
}
$query = "SELECT * FROM table_name"; //pulling data form Access file
$row = odbc_exec($db, $query);
while(odbc_fetch_row($row)
{
$row1 = odbc_result($row,1);
$row2 = odbc_result($row,2);
$row3 = odbc_result($row,3);
echo $row1." ".$row2." ".$row3."<br>"; //watching if data is taken correctly
}
?>
And then you can insert that rows into sql database by adding this code into the while loop :
<?php
$db="MySQLdatabaseName";
$db_connect= mysql_connect($host,$user,$pass);
mysql_select_db($db, $db_connect);
$insert_into_MySQL = "INSERT INTO table_name($row1,$row2,$row3)
VALUES('".$row1."', '".$row2."', '".$row3."'); ";\\These are 2 lines to be
mysql_query($insert_into_MySQL ); \\added to the while loop
?>