• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 576
  • Last Modified:

Temp Table Query Problems

I have these two Access queries that I need to put in a PHP page. The problem is that i cannot figure out how to even get the page to load. I think it's a syntax problem, but this is the first time I've ever tried to use a temp table in Access or from a sql query outside of access. Can you help?


Access Queries:
SELECT AllInfo.* INTO tempTable
FROM AllInfo
WHERE AllInfo.Extension=[Enter Extension Number]
And AllInfo.Date=[Enter Date];
 
SELECT tempTable.Extension, tempTable.Date, tempTable.Time, tempTable.IO, tempTable.AC, tempTable.PhN, tempTable.Duration, (select max([Time]) from tempTable as A where A.Extension =tempTable.extension And  A.Time<tempTable.time and A.[Date]=tempTable.[Date]) AS Time2, format(nz([Time]-[Time2],0),"hh:nn:ss") AS ElapsedTime
FROM tempTable;
 
-------------------------------------------------------------------
PHP Page SQL queries
 
$sql0 = "SELECT * From AllInfo INTO tempTable WHERE Extension = '$ex' AND Date=#$id#";
 
$queryTempTable = "SELECT tempTable.Extension, tempTable.Date, tempTable.Time, tempTable.IO, tempTable.AC, tempTable.PhN, tempTable.Duration, (select max([Time]) from tempTable as A where A.Extension =tempTable.extension And  A.Time<tempTable.time and A.[Date]=tempTable.[Date]) AS Time2, format(nz([Time]-[Time2],0),"hh:nn:ss") AS ElapsedTime
FROM tempTable"
ORDER BY DateTime, Val(Left(Time,2)), Val(Right(Time,2));
 
$sql0 = odbc_exec($odbc,$sql0) or die(odbc_errormsg());   
$queryTempTable = odbc_exec($odbc,$queryTempTable) or die(odbc_errormsg());  
 
echo "<center><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"borderTable\"><tr><th class=\"borderTable\">   Extension   </th><th class=\"borderTable\">     Time     </th><th class=\"borderTable\">In/Out</th><th class=\"borderTable\">Phone Number</th><th class=\"borderTable\">Duration</th><th class=\"borderTable\">Duration</th></tr>";
 
while($row = odbc_fetch_array($queryTempTable)) 
{ 
echo "<tr>";
echo '<td class=\"borderTable\"><center>'.$row['Extension'].'</center></td class=\"borderTable\">'; 
echo '<td><center>'.date("h:i", strtotime($row['DateTime'])).'</center></td>';  
echo '<td class=\"borderTable\"><center>'.$row['IO'].'</center></td class=\"borderTable\">';
echo '<td class=\"borderTable\"><center>('.$row['AC'].') ';
echo ''.$row['PhN'].'</center></td class=\"borderTable\">';
echo '<td class=\"borderTable\"><center>'.$row['Duration'].'</center></td class=\"borderTable\">';
echo '<td class=\"borderTable\"><center>'.$row['ElapsedTime'].'</center></td class=\"borderTable\">';
 
} // end while

Open in new window

0
TTCLIVE
Asked:
TTCLIVE
  • 6
  • 5
3 Solutions
 
Kevin CrossChief Technology OfficerCommented:
Your PHP doesn't quite match your access queries.  For example, the INTO statement of a SELECT...INTO should preceed the FROM statement/clause.

Try this:
PHP Page SQL queries
 
$sql0 = "SELECT * INTO tempTable From AllInfo WHERE Extension = '$ex' AND Date=#$id#";
 
$queryTempTable = "SELECT tempTable.Extension, tempTable.Date, tempTable.Time, tempTable.IO, tempTable.AC, tempTable.PhN, tempTable.Duration, (select max([Time]) from tempTable as A where A.Extension =tempTable.extension And  A.Time<tempTable.time and A.[Date]=tempTable.[Date]) AS Time2, format(nz([Time]-[Time2],0),"hh:nn:ss") AS ElapsedTime
FROM tempTable
ORDER BY DateTime, Val(Left(Time,2)), Val(Right(Time,2))";
 
$sql0 = odbc_exec($odbc,$sql0) or die(odbc_errormsg());   
$queryTempTable = odbc_exec($odbc,$queryTempTable) or die(odbc_errormsg());  
 
echo "<center><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"borderTable\"><tr><th class=\"borderTable\">   Extension   </th><th class=\"borderTable\">     Time     </th><th class=\"borderTable\">In/Out</th><th class=\"borderTable\">Phone Number</th><th class=\"borderTable\">Duration</th><th class=\"borderTable\">Duration</th></tr>";
 
while($row = odbc_fetch_array($queryTempTable)) 
{ 
echo "<tr>";
echo '<td class=\"borderTable\"><center>'.$row['Extension'].'</center></td class=\"borderTable\">'; 
echo '<td><center>'.date("h:i", strtotime($row['DateTime'])).'</center></td>';  
echo '<td class=\"borderTable\"><center>'.$row['IO'].'</center></td class=\"borderTable\">';
echo '<td class=\"borderTable\"><center>('.$row['AC'].') ';
echo ''.$row['PhN'].'</center></td class=\"borderTable\">';
echo '<td class=\"borderTable\"><center>'.$row['Duration'].'</center></td class=\"borderTable\">';
echo '<td class=\"borderTable\"><center>'.$row['ElapsedTime'].'</center></td class=\"borderTable\">';
 
} // end while
 

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
You may also need to escape the double quotes used here:
"hh:nn:ss"

Think in PHP you can just change the outer most quotes to single quote (') then you can use the double quotes (") in query as normal.
0
 
TTCLIVEAuthor Commented:
I fixed the queries with your code and changed the syntax, but I apparently cannot create the temptable in the database because it is read only. So I created a link database so that I can create the table in a separate database but still query the table I need to.

Then when I run the query, it tells me:

[Microsoft][ODBC Microsoft Access Driver] Cannot update. Database or object is read-only.

How do I make the database I created editable so I can run this, and what is the code to drop the table after I create it so I can run this whenever I need to?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kevin CrossChief Technology OfficerCommented:
DROP TABLE tempTable

The above is the syntax for dropping the table.  I don't believe I have had that issue, so I will check and see what I find.  In the meantime, you can double check database security settings and NTFS rights to the MDB file itself as with standard file and default security I usually connect to MDB files for read/write with no issues; however, it has been a while since can use SQL Express in place of Access these days.
0
 
TTCLIVEAuthor Commented:
I checked the NTFS permissions, and just in case while testing added "Everyone" and gave them All permissions.  

Do I need to preface that "DROP TABLE tempTable" with anything in a PHP page, or just drop it at the end of the page inside the "?>" ?

I still get the same error when trying to run the query to either create the temp table or drop it.
0
 
Kevin CrossChief Technology OfficerCommented:
You would run the DROP TABLE sql statement the same way you do the other through an:
odbc_exec(...) or die(...)
0
 
TTCLIVEAuthor Commented:
Okay, so once I get this working I will get the Drop Table working as well. Any ideas on how to allow access to create or drop a table in a link database without getting the "Database or object is read-only." error?
0
 
TTCLIVEAuthor Commented:
It's almost like it's trying to create the temp table in the main database, not the Link Database I created to run all the queries...is there a way to tell is to create the table in this link database rather than trying to do it in the actual database where it doesn't have permissions?
0
 
Kevin CrossChief Technology OfficerCommented:
This should be trying to create the table in whichever database is connected to through the connection string from my understanding.
0
 
TTCLIVEAuthor Commented:
That's what I thought as well, but the database that I created and linked the tables to so that I could run queries is set in a folder that has NTFS permissions for Everyone set to All and there is no password on the database at all, and it still tells me the database is read only whenever I try to create the temp table.

I can run the query directly in Access, but not from an external page.  

So I'm back at square one with a query that works in access directly, but that I cannot run from a web page. Any other ideas on how to accomplish the same goal without trying to use a temp table anyone? Or maybe a way to specify where the temptable should be created?
0
 
TTCLIVEAuthor Commented:
I really need to get htis working, anyone have any ideas? I'm open to suggestions!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now