Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Complete Newbie

Status
Not open for further replies.

chinedu

Technical User
Mar 7, 2002
241
US
Hello all,
I am completely 100% php newbie.

I have been working with asp for a while now and have been presented with the challenge of viewing a pdf with php.

I have done something similar with asp which is this:

Code:
<%


	' Create ADO Connection
	Set Conn = Server.CreateObject("ADODB.Connection")
	Conn.Open "Provider=SQLOLEDB.1;Password=sonn1e;Persist Security Info=True;User ID=sam.okeh; " & _
                          "Initial Catalog=Traf;Data Source=pw2 "
    Response.ContentType = "application/pdf"
	' Create Recordset
	Set RecSet = Server.CreateObject("ADODB.Recordset")
	RecSet.Open "select fileData from TrafficCountsFiles where siteCode ='" & Request("siteCode")& "' ", Conn, 2, 3

	If Not RecSet.EOF Then
     ' set the ContentType property
      Response.Addheader "Content-Disposition", "inline; filename=file.pdf"
     ' send the binary data to the webbrowser
      Response.BinaryWrite (RecSet("fileData"))
	End If

	RecSet.Close
	Set RecSet = Nothing

	Conn.Close
	Set Conn = Nothing

Above code allows me to click on a pdf file and open on a browser to view contents.

Well, since our php guy is on a maternity leave (his wife had a baby) for 3 weeks and we needed to get this done,
I have been trying to re-write it in php.
So far, I am getting errors that I really don't know how to resolve.


I was wondering if someone could be kind enough to help me resolve this.

Here is my table structure.
Table name: Access database
ID autonumber
sitecode text
filesize number
filedata OLE Object (or BLOB)
contentType text
filename text

I just keep getting File does not exist.
I know the reason is because I am using mySQL syntax but my database is not mySQL.

Here is the php version I have been working on.
I truly appreciate any help.

Code:
<?php
// DB connection here.
$conn=odbc_connect('counts','','');

$sql = "SELECT FileData FROM trafficCountsFiles,fctrafic where fctrafic.site_code = trafficcountsFiles.sitecode and siteCode = '".$_GET["siteCode"]."'"; $rs=odbc_exec($conn,$sql);

if (!$rs)
{
 exit("Error in SQL");
}
if (($rs) == 1)

{

$fileType = mysql_result($result, 0, "contentType");
$fileContent = mysql_result($result, 0, "filedata");
header("Content-Type: $fileType");
header("Content-Length: " . strlen($filedata));
header("Content-Disposition: inline; filename=file.pdf;");

echo $fileContent;

}

else

{

echo "Record doesn't exist.";

}

?>
[code]
 
since our php guy is on a maternity leave (his wife had a baby)
Glad you clarified, I was worried for a second. :-D

header("Content-Disposition: inline; filename=file.pdf;");
DId you try to explicitely say where the file was located, directory and all??

___________________________________
[morse]--... ...--[/morse], Eric.
 
Well, the files are on our access database and I am trying to use dsn to access the files:

$conn=odbc_connect('counts','','');
and then once connected to the database, would retrieve it with sql statement.

I am trying to duplicate the asp file.
 
'counts' that the database name or the hostname?

I would add a die for troubleshooting purposes:

$conn=odbc_connect('counts','','') or die mysql_error();



___________________________________
[morse]--... ...--[/morse], Eric.
 
errr, i forgot, your using access, so it would be:
$conn=odbc_connect('counts','','') or die odbc_error();


___________________________________
[morse]--... ...--[/morse], Eric.
 
Thanks for your response.
Now I am getting this error:
This error:

Parse error: parse error, unexpected T_STRING in e:\inetpub\ on line 13

and the error is this line:

Code:
$rs = odbc_connect($sql) or die odbc_error();

Below is the rest of the connection and recordset
Code:
$conn=odbc_connect('counts','','');
$sql = "SELECT FileData FROM trafficCountsFiles,fctrafic where fctrafic.SITE_CODE = trafficcountsFiles.sitecode and fctrafic.SITE_CODE = '".$_GET["SITE_CODE"]."'"; $rs=odbc_exec($conn,$sql);
$rs = odbc_connect($sql) or die odbc_error();
 
Here ya go:
Code:
$connection_string = 'DRIVER={SQL Server};SERVER=<servername>;DATABASE=<databasename>';

$user = 'username';
$pass = 'password';

$connection = odbc_connection( $connection_string, $user, $pass ) or die(odbc_error());
$sql = "SELECT FileData FROM trafficCountsFiles,fctrafic where fctrafic.SITE_CODE = trafficcountsFiles.sitecode and fctrafic.SITE_CODE = '".$_GET["SITE_CODE"]."'"; $rs=odbc_exec($connection,$sql);

You have to set your DSN info like that.

___________________________________
[morse]--... ...--[/morse], Eric.
 
Thanks for being so patient and helpful but I am not using sql server.
As I indicated in my first post, I am using Access database and I have not worked out how to use access database so far.

Also, I have been doing some research since my first post today and I found that if I use this syntax:

Code:
error_reporting(E_ALL & ~E_NOTICE);

then it would hide the notices I have been getting and throw out the real error message.

I did that and now I getting:
Site_Code does not exist.

Well, it does exist

As you can see, I am joining 2 tables:
fctrafic and trafficCountsFiles.

I have already given the data structure for trafficCountsFiles.

The data structure for fctafic is:
site_code,
location,
distance.

Now what happens is that I have a page which has this code snip:

Code:
<td class="name">Site Code </td>
<td class="name">Distance </td>
<td class="name">&nbsp;</td>


<td class="value">[SITE_CODE]</td>
<td class="value">[DISTANCE]</td>
<td class="name" width="10%" align="center" valign="top"><font size="-1"><a href="#" onClick="window.open('[URL unfurl="true"]http://MyDomain/reports.php?site_code=[/URL][SITE_CODE]','','width=500,height=200,scollbars,resizable=yes')">Study Info</a></font></td>

In theory, if you click on the link that says Study Info, based on the site_code being passed, records associated with it will be pulled.

What am I doing wrong?
 
I cant see where anything is wrong, but you have to specify a DSN in that odbc string. The one foR ACCESS is:

Microsoft Access Driver (*.mdb);

If you do not have the proper ODBC driver installed, you need to configure the script to do so:

$connection_string = 'DRIVER={Microsoft Access Driver (*.mdb)};
SERVER=<servername>;
DATABASE=<databasename>;
DBQ=C:/Access.mdb; //location of your database
UserCommitSync=Yes;
Threads=3;
SafeTransactions=0;
PageTimeout=5;
MaxScanRows=8;
MaxBufferSize=2048;
DriverId=281;
DefaultDir=C:/ProgramFiles/CommonFiles/ODBC/DataSources"
';
[/code]


___________________________________
[morse]--... ...--[/morse], Eric.
 
Still struggling mightily with this code.
Here is the latest "real" code followed by a confluence of error messages.

I am truly screaming help!

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>More Counts</title>
<link rel="stylesheet" type="text/css" href="/ms/style_css/styles.css">
</head>
<body bgcolor="#6270B5">
<div align="center">
<?php
// DB connection here.
$connectionstring = odbc_connect("counts", "", "") or die(odbc_error());
$sql = "SELECT FileData FROM trafficCountsFiles,fctrafic where fctrafic.site_Code = trafficcountsFiles.sitecode and fctrafic.site_Code = '".$_REQUEST["site_Code"]."'"; $rs=odbc_exec($connectionstring,$sql);
if (!$rs)
{
 exit("Error in SQL");
}
if ($rs)

{

$fileType = odbc_result($result, 0, "contentType");
$fileContent = odbc_result($result, 0, "filedata");
header("Content-Type: $fileType");
header("Content-Length: " . strlen($filedata));
header("Content-Disposition: inline; filename=file.pdf;");

echo $fileContent;

}

else

{

echo "Record doesn't exist.";

}

?>
</body>
</html>

Notice: Undefined index: site_Code in e:\inetpub\ on line 10

Notice: Undefined variable: result in e:\inetpub\ on line 13

Warning: Wrong parameter count for odbc_result() in e:\inetpub\ on line 13

Notice: Undefined variable: result in e:\inetpub\ on line 13

Warning: Wrong parameter count for odbc_result() in e:\inetpub\ on line 13

Warning: Cannot modify header information - headers already sent by (output started at e:\inetpub\ in e:\inetpub\ on line 13

Notice: Undefined variable: filedata in e:\inetpub\ on line 13

Warning: Cannot modify header information - headers already sent by (output started at e:\inetpub\ in e:\inetpub\ on line 13

Warning: Cannot modify header information - headers already sent by (output started at e:\inetpub\ in e:\inetpub\ on line 13


From what I have read so far, it isn't a good idea to supress the error messages.
 
Deal with the errors in the order in which they are presented. The "cannot modify header" errors will likely go away once you have worked around the previous errors.

the "site_Code" error......
First, I strongly recommend that you not use $_REQUEST in your code. It has some of the security issues as running PHP with register_globals set to "on"
. Use either $_GET or $_POST as appropriate instead.

Second, is a form being submitted to this script? Does the name of the field case-specificially match the name of the value you're trying to reference?



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
No, no form is being submitted to this script, if I understand it correctly.
What happens is that there is a form:

Code:
<td class="name">Site Code </td>
<td class="name">Distance </td>
<td class="name">&nbsp;</td>


<td class="value">[SITE_CODE]</td>
<td class="value">[DISTANCE]</td>
<td class="name" width="10%" align="center" valign="top"><font size="-1"><a href="#" onClick="window.open('[URL unfurl="true"]http://MyDomain/reports.php?site_code=[/URL][SITE_CODE]','','width=500,height=200,scollbars,resizable=yes')">Study Info</a></font></td>

when you click the link titled "Study Info", the site_code is supposed to be passed to this script and records associated with it are then supposed to be displayed to the screen.
So far, I am stuck with these silly error messages.
 
Again, what is the name of the form-field in the first error?

Is it "site_code" or "site_Code"? Variable names and associative array indeces are case-specific in PHP.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I posted twice here already. Here it is again:

<a href="#" onClick="window.open('[SITE_CODE]','','width=500,height=200,scollbars,resizable=yes')">Study Info</a></font></td>

Just so you know, I have tried
reports.php?site_Code = [site_Code] but still get same error messages.
 
If you're calling the field "site_code" in your link, you must refer to it as $_GET['site_code'] in your script. $_GET['site_Code'] does not exist, as it is a different variable from $_GET['site_code'].



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I have managed to rid the code of all the errors and warnings I have been getting but here is my question.

When I try running the code, the value placed in site_Code is [SITECODE].
Everything else is blank.
That tells me that something is wrong with the value being passed to site_Code from the form script.

For instance, this is the code from form:

"><a href="#" onClick="window.open('[SITE_CODE]

The site_Code here is being passed to script I posted earlier and that script is supposed to be picking up the site_Code and associating the rest of the records with it like this:

Code:
$sql = "SELECT fctrafic.*,contentType, FileData FROM trafficCountsFiles,fctrafic where fctrafic.site_Code = trafficcountsFiles.sitecode and fctrafic.site_Code = '".$_GET["site_Code"]."'";
but when I run the code, all values are blank (This is correct because database is blank) but the site_Code displays like this:

Site Code = [SITE_CODE]
This tells me there is something wrong with the syntax,
Can anyone please tell?

This is my most current code:

Code:
<?php
// DB connection here.

$connectionstring = odbc_connect("counts", "", "") or die(odbc_error());

$sql = "SELECT fctrafic.*,contentType, FileData FROM trafficCountsFiles,fctrafic where fctrafic.site_Code = trafficcountsFiles.sitecode and fctrafic.site_Code = '".$_GET["site_Code"]."'";       // Not, site_Code

$result=odbc_exec($connectionstring,$sql);

if ($result)
{
$fileType = odbc_result($result, "contentType");
$fileContent = odbc_result($result, "FileData");

header("Content-Type: $fileType");
header("Content-Length: " . strlen($fileContent));
header("Content-Disposition: inline; filename=file.pdf;");

?>
<html>
<head>
<title>More Counts</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body bgcolor="#6270B5">
<div align="center">

<?php
echo $fileContent;

}

else

{

echo "Record doesn't exist.";

}

echo "  <div align=\"center\">
 <table cellpadding='2' cellspacing='2'>
<td class='header'><font size='-3'>SITE CODE</font></td><td class='name'><font size='-3'>" . ((isset($site_Code)) ? $site_Code : 'No Site Code Found') . "</font></td><td class='header'><font size='-3'> TYPE</font></td><td class='name'><font size='-3'>" . ((isset($types)) ? $types : 'Type Not Available') . "</font></td></tr>
<td class='header'><font size='-3'>AADT</font></td><td class='name'><font size='-3'>" . ((isset($aadt)) ? $aadt : 'No Record Found') . "</font></td><td class='header'><font size='-3'> DATE</font></td><td class='name'><font size='-3'>" . ((isset($fdate)) ? $fdate : 'No Date Available') . "</font></td></tr>
<tr><td class='header'><font size='-3'>LOCATION</font></td><td class='name'><font size='-3'>" . ((isset($location)) ? $location : 'No Record Found') . "</font></td><td class='header'><font size='-3'>DISTANCE</font></td><td class='name'><font size='-3'>" . ((isset($distance)) ? $distance : 'No Record Found') . "</font></td></tr>
<tr><td class='header'><font size='-3'>REF LOCATION</font></td><td class='name'><font size='-3'>" . ((isset($location)) ? $location : 'No Record Found') . "</font></td><td class='header'><font size='-3'>DIRECTION</font></td><td class='name'><font size='-3'>" . ((isset($direction)) ? $direction : 'No Record Found') . "</font></td></tr>
<tr><td class='header'><font size='-3'>FILE NAME</font></td><td class='name'><font size='-3'>" . ((isset($filename)) ? $filename : 'No File(s) Found') . "</font></td></tr>
</table><br>
<button name=\"close\" value=\"Close\" onClick='window.close()'>Close</button>
</div>";

?>
</body>
</html>
 
Where are passing any other values to the script?

I see explicit GET-method input here:

<a href="#" onClick="window.open('[SITE_CODE]

But that input only includes a value for the variable $_GET['site_code']. Where would the rest of the variables be coming from?



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Unless it is done differently with php, I thought that once you pass the value of site_Code from the form to the other script, based on that value, the rest of the values from come from this code:

Code:
$sql = "SELECT fctrafic.*,contentType, FileData FROM trafficCountsFiles,fctrafic where fctrafic.site_Code = trafficcountsFiles.sitecode and fctrafic.site_Code = '".$_GET["site_Code"]."'";

This way, if for instance, the value of site_Code passed from the form to this code is 123, then whatever records are associated with 123 from this query will be passed to $types, $location, $distance, $filename, etc.
am I off track with that logic?

I know this would work beautifully with asp.

But back to my original question, though, when I run my code, I see:

site Code Distance Direction Date File Name
[SITE_CODE]

Notice that only site Code has a value everything else is blank which is fine because as said, no records in the database right now.
But what is interesting here is that the value of Site Code is [SITE_CODE] which is exactly the way it looks from the form :
<a href="#" onClick="window.open('[SITE_CODE]

Does it mean this is correct and the rest are not, or the rest is correct but this is not.
Please tell me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top