1

Firslty, please forgive the ineloquent phrasing of my question.

What I am trying to do, is create a simple odbc script, and store it in a file which will be included in my main PHP file several times with different variables.

When including them, I want to specify some variables to pass to it.

My example would be:

Main page

include("table-fields.php?table=sometable");

table-fields.php

$table = $_GET['table'];
odbc_exec(some database function WHERE TBL= $table);

However, if my understanding is correct, as $_GET is global, it would be looking for main.php?table=

Would the better choice be to just set the variable before including, e.g.:

$table = some table;
include("table-fields.php");

table-fields.php

odbc(some database function WHERE TBL= $table);

I want to try and avoid that if possible.

Thanks, Eds

3
  • $table = $_GET['table']; odbc_exec(some database function WHERE TBL= $table); should work... Commented May 8, 2013 at 14:31
  • May be a duplicate stackoverflow.com/questions/5998705/… Commented May 8, 2013 at 14:31
  • Reading a file from your hard disc is an entirely different operation than downloading a file from the Internet. Perhaps you're confused because include() can actually do both things. Commented May 8, 2013 at 14:38

2 Answers 2

2

When including a file, the contents of that file is outputted into the current file, it's not requested with HTTP, so all you need to do is :

$table = "sometable";

include("table-fields.php");

and in the included file, just use the variable :

odbc(some database function WHERE TBL= $table);

as the included content would work just like if you wrote it in the main file etc.

Sign up to request clarification or add additional context in comments.

3 Comments

The odd thing is, that in another page on our site, I have a script to get a user image from the database. This is done in the same way, where I include("getimage.php?id=x"). Then, inside that script, I have a $_GET['id'] which then queries the database. This works for all IDs. The only difference, is that at the end of that script, I set the header("type: image/jpeg") and then echo the content. Is it possible to do something similar in this case? Thanks
@Eds - The point is, you don't have to use the querystring to get data when using PHP includes, and you should'nt, as the included file will be part of the including file. You only need querystrings when using anchors etc on your page to request an image based on the ID etc, not when doing includes.
Ok no problem. I suppose it's neater to do it this way anyway. Thanks
0

You have to declare the variable before the include and it will be available in the code under it.

$_GET is used to get data from HTTP request.

As you pointed out, this would be the correct way:

$table = some table;
include("table-fields.php");

Just imagine the include as a copy and paste inside your code. The code inside the included content will be replazing the include call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.