How to have PHP interact with an Access 2007 database directly? - php

I have Access 2007 installed and I downloaded the Northwind Traders sample database which is easily available on the Internet.
I want to open connection with this database in PHP so that I can practice my PHP coding. I want to directly access the database without having to export it to MySQL and then accessing it.
I followed the following steps from http://www.w3schools.com/php/php_db_odbc.asp to achive that:
Open the Administrative Tools icon in your Control Panel.
Double-click on the Data Sources (ODBC) icon inside.
Choose the System DSN tab.
Click on Add in the System DSN tab.
Select the Microsoft Access Driver. Click Finish.
In the next screen, click Select to locate the database.
Give the database a Data Source Name (DSN).
Click OK.
Then I tried the following code in php :
<?php
$conn=odbc_connect('northwind','','');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
?>
But it is displaying "Connection Failed".
Please advise me how I can connect to Northwind database directly.

Related

Issues accessing the local database (LocalDb)\v11.0

I'm trying to install a php script locally on IIS.
I've gotten to the point where I need to fill in my database information.
I've connected to another database through visual studio, here's the connection string:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\TaskSubmitter.mdf;Initial Catalog=TaskSubmitter;Integrated Security=False; User Id=sa; Password=thepassword"
Now I've created a database named "pligg" for the php script.
The information I've filled in is the following:
Database name: pligg
Database username: sa
Database password: thepassword
Database server: (LocalDb)\v11.0
Table prefix: _pligg
However I get an error when trying to connect through the script.
The script is working fine so I'm sure I've done something wrong regarding the settings or the database.
Anyone know what could be wrong?

php excel com_exception: unable to lookup 'Workbooks': Access is denied

I am trying to use PHP's COM interface to query a database and create pivot tables from the data in an Excel Workbook.
I have configured the DCOM settings on the local machine to grant the user Local Access, Local Launch, and Local Activation privileges as well as Full Control of Configuration Permissions. I have also tried all three Identity settings.
Here is the code I am using:
<?php
$application = new COM("Excel.Application") or die("Unable to load Excel.");
$workbook = $application->Workbooks;
$workbook = $workbook->Add();
$sheet = $workbook->Worksheets(1);
$sheet->Activate();
$application->Quit();
?>
This is the error I am getting:
PHP Fatal error: Uncaught exception 'com_exception' with message 'Unable to lookup `Workbooks': Access is denied.
This is different than error I was getting before which told me that I didn't have access to the Excel COM object. Setting the permissions fixed that.
Environment is:
Windows Server 2012 R2
Excel 2010 (64-bit)
PHP 5.5.9 (64-bit)
I never was able to solve this, so we had to come up with a different solution and it actually is more stable and performs better. Hope it helps anyone else that may run into the same issue.
Our database is MySQL (but this should be possible with most databases), so we moved our query into its own script and used the mysql client to write the results directly to a file.
We then created an excel template with a data connection to the file and used the connection as the data source for the pivot tables.
Lastly we wrote a short vbscript to open the template, refresh the pivot tables, then and save the file as a new copy.
This had the added benefit of allowing us to make stylistic changes using Excel's interface, which was much faster than doing it programmatically.

Connect to MySQL Data Source in PHPStorm

I'm using an IDE "Phpstorm" with "wampserver". i want to connect with MySQL data source, but i'm getting a problem:
Connection to MySQL - DatabaseName#localhost failed: Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.intellij.persistence.database.console.RemoteJdbcServer.main(RemoteJdbcServer.java:15)
I also downloaded mysql-connector-java-gpl-5.1.29.msi and installed it, but I'm still getting some problems.
When I had this issue, there was a section of the Data Sources and Drivers Dialogue box that said "Driver Files" I expanded it and there was a link to download driver files.
I clicked it - and after a few seconds (with no obvious visual progress indicator) the message changed to say using MySQL drivers.
After that it worked.
Short version:
Open the Database tab, usually located on the very upper right of
PHPSTORM.
Click on the small wrench (Data Source and Property)
In this new popup window choose MySQL from the left.
Delete Red driver files with the minus symbol
Click Download
From there on you should be good to go. (phpstorm handles the rest)
Detailed version:
Open the Database tab, usually located on the very upper right of PHPSTORM.
Click on the small wrench (Data Source and Property)
New Version v2020:
If it's not there open it from
View -> Tool Windows -> Database
New Version v2020
- In this new popup window choose MySQL from the left.
New Version v2020:
- Now you should be on the settings tab where you can see driver
files, if any of them are red just delete them with the
minus symbol, as soon as there are no driver files left you are able to click on download.
If any of the above fail try these:
Delete the drivers in use and reinstall. Usually located at ~/.PhpStormXXXX/config/jdbc-drivers.
After that repeat the first instructions in this post. (short version)
Using a proxy?
Make sure you have configured it within phpstorm!
At Appearance & Behavior -> System Settings -> HTTP Proxy.
Make sure you have the latest drivers:
Create a new mysql datasource, if the driver needs an update it will display it on the very bottom of the popup
try other versions
New Version v2020:
Installing the Driver Manually
I couldn't find any official Jetbrains mysql driver repo page, but these urls are official and work:
http://download.jetbrains.com/idea/jdbc-drivers/mysql-connector-java-5.1.35-bin.jar
http://download.jetbrains.com/idea/jdbc-drivers/mysql-connector-java-5.1.40-bin.jar
http://download.jetbrains.com/idea/jdbc-drivers/mysql-connector-java-5.1.44-bin.jar
http://download.jetbrains.com/idea/jdbc-drivers/mysql-connector-java-5.1.45-bin.jar
http://download.jetbrains.com/idea/jdbc-drivers/mysql-connector-java-5.1.46-bin.jar
http://download.jetbrains.com/idea/jdbc-drivers/mysql-connector-java-5.1.47-bin.jar
http://download.jetbrains.com/idea/jdbc-drivers/mysql-connector-java-5.1.48-bin.jar
The Newest version number can be found here:
https://dev.mysql.com/downloads/connector/j/5.1.html
just replace the [VERSION] with the current version e.g.
http://download.jetbrains.com/idea/jdbc-drivers/mysql-connector-java-[VERSION]-bin.jar
Add the downloaded file with clicking on the small grey plus. (in older versions green and on the right)
New Version v2020:
Or try the official mysql repo (caution you MUST register to download!):
Go to http://dev.mysql.com/downloads/connector/ within the dropdown select "Platform Independent" it will show you the options to download tar or zip files.
Download one of them and extract it,
Within the resulting folder you will find a mysql-connector-XXX.jar file.
Add it to phpstorm in the mysql datasource dialog box.
Other stuff:
Useful phpstorm UI reference link
Code Monkey's answer pointed me in the right direction, but my firewall blocked the download from within PHPstorm. Luckily the error message included the download URL to find the driver.
So I downloaded the driver with help from the URL, and then added it manually:
PHPStorm 2020.1 with Material Dark theme:
Hit Alt+0 or View→Tool Windows→Database to open the Database tool window.
Click the pancakes-with-wrench icon
Click the TEST CONNECTION button
This will prompt you to download the drivers - do it
You will have to enter your root password again
Had the same issue. I resolved it in the Data source and drivers window on panel Mysql. There is checkbox "Use provided driver" and next to it there was link "Version 5.1 (latest)". I clicked on it a now it works.
Anyone else who came here trying to locate the Database tab in the latest version of PhpStorm (Version 2017.1), which is hidden at first, try the following, taken from PhpStorm Website
Database support is provided through the Database Tool Window on the right-hand side of the IDE. Use the View | Tool Windows | Database menu or Find Action, by pressing Ctrl+Alt+A (Alt-CMD-A on Mac OS X) and search for "Database" to open it.
if nothig of above works, you can delete .PhpStorm2017.3/config/jdbc-drivers folder. then navigate to your db properties and download the driver again

WebMatrix 3 not showing MySQL databases

I am new to WebMatrix and MySQL, and I need some assistance with an issue.
I'm trying to code with PHP and MySQL in WebMatrix 3. I am experimenting with scripts in a site file called "Starter PHP site". WebMatrix is not showing a database created with the $sql="CREATE DATABASE my_db"; script when I navigate to the "Databases" section in the program. This image shows what part of the program I'm talking about.
(source: orchardproject.net)
When I click the "Databases" button in the lower left corner of the UI, all that is shown in the left pane is a "Starter PHP site" folder icon (for creating a MySQL databse for my site from the WebMatrix UI) and an "Other Connections" icon -- but not the database created (my_db) after running the script above.
I know that the database exists...because when I run the script above again with some extras--
$con=mysqli_connect("localhost","xxxx","xxxxxxxx");
$sql="CREATE DATABASE my_db";
if (mysqli_query($con,$sql))
{
echo "Database my_db created successfully";
}
else
{
echo "Error creating database: " . mysqli_error($con);
}
--I get an error saying that "my_db" already exists. Why doesn't WebMatrix show my_db in the "Databases" section...after I've run the script to create the database?
What am I missing here?
Any advice is greatly appreciated.
If it's there you should see it as such:
Dumb question, but have you tried refreshing?
It seems that I have found some sort of solution. Apparently, WebMatrix 3 might not show MySQL databases created with PHP scripts automatically, but you can view databases created with scripts by creating a new MySQL database connection to the database of interest from the WebMatrix GUI.
Solution: Navigate to the "Databases" section.... Select "New," then..."MySQL Connection." Then, enter the database information.... You should now have a working database connection.
...sorry if this is one of those newbie mistakes. I should have tried this earlier, but one might think that, since one has to establish a database connection in the PHP code, WebMatrix would automatically connect to it. For me, that isn't the case.

php using msaccess

Because no one answer my question Codeigniter using ms access database , i make new question i not using codeigniter
if I using mdb from my computer it works but if i using mdb from another computer in my network
i got error this is my CODE
<?php
$connect = odbc_connect("testdb", "", "");
$query = "SELECT * FROM ACGroup";
$result = odbc_exec($connect, $query);
while(odbc_fetch_row($result)){
$name = odbc_result($result, 1);
echo("$name");
}
?>
i make odbc where the data its from another computer in my network
"testdb" are data sourcename and database \\ip\folder\testdb.mdb
and i got this error
Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Microsoft Access Driver]
The Microsoft Jet database engine cannot open the file '(unknown)'. It is already opened exclusively by another user, or you need permission to view its data.,
SQL state S1000 in SQLConnect in D:\blabla\coba.php on line 3
I already try folder setting but it not work
The ODBC connection string you are using ("testdb") does not even specify a database driver, much less a database file. You will need to read up on the appropriate format for $dsn. (The php.net page even has a few examples for Access files.)
The problem possibly lies in the permissions your Apache user has on the mdb file.
Go to your mdb file, right-clik on the file -> Properties ->Security tab and see if your Apache user is on the User Groups that have access/control on the mdb file.
In order to see/change your Apache user, go to Control Panel-> Administrative Tools -> Services, right-click on Apache service -> Properties -> Logon tab. In the Log on as option, see if the user selected belongs to the User Groups that have access to mdb file. If not, create one and add it to the User Groups and then use it as the Apache account.

Categories