Solved

Reading data from Excel via ODBC

Posted on 2009-06-29
10
964 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 19

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
 
LVL 19

Assisted Solution

by:NerdsOfTech
NerdsOfTech earned 200 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 19

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Accepted Solution

by:
RQuadling earned 300 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 19

Expert Comment

by:NerdsOfTech
ID: 24744674
Thanks RQ
0
 
LVL 40

Expert Comment

by:RQuadling
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 19

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now