Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading data from Excel via ODBC

Posted on 2009-06-29
10
Medium Priority
?
984 Views
Last Modified: 2013-12-13
Hi I'm trying to connect a PHP file to an Excel file, all on a remote Windows server.

So I've uploaded a file called "Book2.xls" into a folder called "testfolder".

DNS is called "test".
DBQ is hshome\myhostingpackage\mywebsite.com\testfolder\Book2.xls
where the relevant values have been changed to my own.

My code is as below (nicked from another post on EE) but it gives the following error, can someone please help???? Many thanks:

Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Excel Driver] '(unknown)' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides., SQL state S1009 in SQLConnect in D:\hshome\myhostingpackage\mywebsite.com\testfolder\test.php on line 3

Warning: odbc_tables(): supplied argument is not a valid ODBC-Link resource in D:\hshome\myhostingpackage\mywebsite.com\testfolder\test.php on line 5

Warning: odbc_fetch_row(): supplied argument is not a valid ODBC result resource in D:\hshome\myhostingpackage\mywebsite.com\testfolder\test.php on line 7

<?
$hCon=odbc_pconnect("test", "","");
$hStmt=odbc_tables($hCon);
 
while(odbc_fetch_row($hStmt))
{
      echo "<h1>Contents of Worksheet ".odbc_result($hStmt, "TABLE_NAME")."</h1>";
 
      $sql="SELECT * FROM [".odbc_result($hStmt, "TABLE_NAME")."]";
 
      $hStmt2=odbc_exec($hCon, $sql);
 
      odbc_result_all($hStmt2);
 
}
?>

Open in new window

0
Comment
Question by:kbit
  • 5
  • 3
  • 2
10 Comments
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24742446
make sure it is a system DSN (ODBC) in Windows, not user DSN.
0
 

Author Comment

by:kbit
ID: 24742922
Attached is my screenshot of the ODBC setup page
dns.jpg
0
 

Author Comment

by:kbit
ID: 24743182
Is there a way (using PHP) to fully set up all the ODBC parameters in code instead of using the screen I've shown? Thanks
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 20

Assisted Solution

by:NerdsOfTech
NerdsOfTech earned 800 total points
ID: 24743330
Test #1
$conn=odbc_connect('test','','');
$query="SELECT * FROM sheet1";
$result=odbc_exec($conn,$query);
while (odbc_fetch_row($result))
{
  $field1=odbc_result($result,1);
  echo $field1 . '<br/>';
}
odbc_close($conn); 

Open in new window

0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24743369
There is probably an another way to do it; however, I have not found a way other than admin to create the connection.

I suggest that you convert your data to mysql instead for increased flexibility and scalability nonetheless so that you can utilize the suitability of table structures etc.
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 1200 total points
ID: 24744535
You don't need a DSN. You can use a DNS-less connection.See Example #1 DNS-less connections in http://docs.php.net/odbc_connect.
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24744674
Thanks RQ
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 24744730
Once you've got the connection, there are several "table" types.

A worksheet is a "table", but so are other things like named lists, etc.

I think you have to use table names like this ...

'sheet1$'

With the quotes and the $ symbol.

The quotes may be optional for sheets without a space in the name.
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24744986
DNS-less

Nice!
// Microsoft Excel
$excelFile = realpath('C:/ExcelData.xls');
$excelDir = dirname($excelFile);
$connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');

Open in new window

0
 

Author Comment

by:kbit
ID: 24745301
I got it to work without the DNS being set up!!!

Below is the winning code, many thanks for all the suggestions and help.

<?
 
// Microsoft Excel
$excelFile = realpath(D:\hshome\myhostingpackage\mywebsite.com\testfolder\Book2.xls');
$excelDir = dirname($excelFile);
$connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');
 
$query="SELECT * FROM [names$]";
$result=odbc_exec($connection,$query);
while (odbc_fetch_row($result))
{
  $field1=odbc_result($result,1);
  echo $field1 . '<br/>';
}
odbc_close($connection);
 
?>

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question