Solved

Reading data from Excel via ODBC

Posted on 2009-06-29
10
961 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 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

20 Experts available now in Live!

Get 1:1 Help Now