ms-access database to mysql database by php - php

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
?>

Related

mysql - create event and load data infile to Server automatically

I need to know if it's possible to Create an Event in MySQL with its code statement using Load Data In file syntax.
Here's my current situation.
I can get the database (MS Access) using PHP from a Local Biometric Database.
I have updated the database in my Local Database Server which is MySQL now.
I can update my target Online Database Server via Exporting it first then Importing the database in the Online Database Server.
My Main Question is how I can manage this without any user interaction
such as MySQL Event Features?
I am trying to upload a data from my PC (Local server) into our Domain (Web Server)
Already done this guys, What i did is I've built a local application to my local server at our office then that application is doing a FTP service to upload the updated database from our Local Biometric Database into the online web server.
Once all this is done, I've created a php script which will load the .SQL file using a Cron Job. You all know cron job are time scheduled events for controlling scripts and doing other linux command.
I'll share my code of php file so everyone who encounter this can use this in the future.
date_default_timezone_set('Asia/Taipei');
$CurTime = date('Y-m-d H:i:s');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link){
die('Failed to connect to server: ' . mysqli_error());
}
//Select database
$db = mysqli_select_db($link,DB_DATABASE);
if(!$db){
//Do auto download of database in the Server automatically
if(file_exists('Database/'.DB_DATABASE.'.sql')){
mysqli_query($link,"CREATE DATABASE ".DB_DATABASE);
$db = mysqli_select_db($link,DB_DATABASE);
$readContent = file_get_contents("Database/".DB_DATABASE.".sql");
$res = mysqli_multi_query($link,$readContent) or die(mysqli_error($link));
mysqli_close($link);
} else {
die("Unable to select database, Please call your system administrator. Thank you.");
}
}
function Reload_Database($DBLink){
if(file_exists($DBLink)){
//mysqli_query($link,"DROP DATABASE ".DB_DATABASE);
//mysqli_query($link,"CREATE DATABASE ".DB_DATABASE);
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db = mysqli_select_db($link,DB_DATABASE);
$readContent = file_get_contents($DBLink);
$res = mysqli_multi_query($link,$readContent) or die(mysqli_error($link));
mysqli_close($link);
return 'Database Reloaded';
} else {
die("Unable to reload database, Please call your system administrator. Thank you.");
}
}
echo Reload_Database('../employee/database/'.DB_DATABASE.'.sql');
This is basic script that will reload the database of the uploaded database given from my local application.
From here the cron job will do its part. Expect that my local application is also a service that will just do the uploading of the updated database of the Local Biometric Database.

Database connection phpmyadmin

I'm working for an e-commerce that has the db on phpmyadmin. In another website I'd like to connect to that database. I have password, username and db name, so I'm using this "connection string":
<?php
$nomehost = "localhost";
$nomeuser = "user";
$password = "pass";
// connection
$conn=mysql_connect($nomehost,$nomeuser,$password);
if (!$conn) exit ("Error connection<br>");
// db name
if (!mysql_select_db("db_name",$conn)) exit("Error db name<br>");
?>
The result is "Error db name". What can I do? Have I to set some oprion in the phpmyadmin?
First of all:
this error is caused by the fact that you are selecting the wrong database in your MySql server. Is your db called db_name???
EDIT: based on the comments you are making: is the server that hosts the php page the same as the mysql server?
Then:
phpmyadmin is just a tool to connect and handle MySql databases and is not a database server itself.
Last but most important:
you are using a deprecated library (mysql) in php to connect to a MySql server. Please consider moving to mysqli or better to PDO

How to connect php to a database in vb application?

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.

How to retrieve data for a webpage from an MS Access database with password

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't access the SQLite database with MAMP and PHP

I have been learning how to program websites lately and the time has come for me to add a database. I have in fact already successfully created a MySQL database and interacted with it with PHP.
My problem is I can't seem to access a SQLite database file with it. I am using MAMP to host locally for now. Here is a snippet of the code I am using to access the db and find and print out a value stored on it.
<?php
$dbhandle = sqlite_open('/Applications/MAMP/db/sqlite/Users');
if ($dbhandle == false) die ('Unable to open database');
$dbquery = "SELECT * FROM usernames WHERE username=trevor";
$dbresult = sqlite_query($dbhandle, $dbquery);
echo sqlite_fetch_single($dbresult);
sqlite_close($dbhandle);
?>
As you have access to the database (your code doesn't die), I'd say there's got to be an error later ;-)
Looking at your SQL query, I see this :
SELECT * FROM usernames WHERE username=trevor
Are you sure you don't need to put quotes arround that string ?
Like this :
SELECT * FROM usernames WHERE username='trevor'
Also, notice that sqlite_fetch_single will only fetch the first row of your data -- which means you might need to use sqlite_fetch_array or sqlite_fetch_object if you want to access all the fields of your resulset.

Categories