?
Solved

Reading data from Excel via ODBC

Posted on 2009-06-29
10
Medium Priority
?
975 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 19

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

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

752 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