4

I have a situation where I would like to use a php file "query.php" to look take either a $_POST or $_GET value as the MySQL query. It looks like this:

<?php
//verify data has been sent via POST or GET and set strings
//find supplied table name
if(isset($_POST['tblName'])){
    $strSuppliedTableName = $_POST['tblName'];
}
if(isset($_GET['tblName'])){
    $strSuppliedTableName = $_GET['tblName'];
}
else{
    $strSuppliedTableName = 'roles';
}

//find supplied field name or default to all fields in the table
if(isset($_POST['fieldName'])){
    $strSuppliedFieldName = $_POST['fieldName'];
}
else if(isset($_GET['fieldName'])){
    $strSuppliedFieldName = $_GET['fieldName'];
}
else{
    $strSuppliedFieldName = '*';
}

//query db
$query = 'SELECT ' . $strSuppliedFieldName . ' FROM ' . $strSuppliedTableName;

$results = mysql_query($query) or die(mysql_error());
?>

Following that, I want to include this file "query.php" in another file that will manage the results. I'm trying to make this as modular as possible.

<?php
require_once("query.php?tblName=classes");
......... (while loop, yadi yadi

However, I recieve an error:

Warning: require_once(query.php?tblName=classes) [function.require-once]: failed to open stream: No such file or directory

Is it not acceptable to pass GET values to your included file? PHP won't process this?

1
  • I have a sneaking suspicion that if you included the query.php file on a page and tried referencing "get" variables, php would actually look to the url of the page you are currently on instead of query.php. One thing you could do would be to just set the variables in the page that calls query.php above the "include 'query.php'" line and then reference said variables within query.php. Commented Jun 19, 2011 at 16:56

4 Answers 4

6

You do not need to pass variables in a get or POST like way when including or requiring files, the variables are shared between the files, as long as the values are set before the including happens.

i.e.:

file1.php called as file1.php?var2=value2

<?php
$var1 = "value1";
$var2 = $_GET['value'];
include "file2.php";
?>

file2.php:

<?php
echo $var1.' '.$var2;
?>

will output:

value1 value2

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

2 Comments

This is perfect. Can't believe I didn't think of that :) I guess I was so absorbed in the idea of reinventing the wheel over here that I didn't consider that. This = brilliant, thanks a lot
in my case it is not working. i used POST and require_once remains everything is same.
1

As a shortcut, you can use $_REQUEST inside, which is an amalgam of the _GET, _POST, _COOKIE, and _ENVIRONMENT superglobals. Exactyl which ones go into it is under control of the request_order .ini setting.

Alternatively, an utterly reliable method to check which METHOD you're handling is $_SERVER['REQUEST_METHOD']. This value is always set when handling an HTTP request, and will be GET, POST, HEAD, etc... Unlike checking for the presence of a form field, it's utterly dependable - the form field may not be submitted (unchecked checkbox?), it may be renamed in the HTML but you forget to change the script, etc...

As for your require(), unless you specify an absolute url (http://...), PHP will interpret its argument as a request for a local file, and will not pass it through the HTTP layer. Unless you have a file named query.php?tblName..., it'll be "file not found" and the require() fails.

2 Comments

I appreciate that advice! I have heard or read somewhere though that $_REQUEST is less secure than using specific methods alone. However, based on your suggestion I will look into it! Thanks again
It's insecure in that you don't know precisely where a particular value came from, since a request method later in the request_order will overwrite earlier values. If you need a value from a specific request method, then use that particular superglobal. If you're doing something like a search script and don't particularly care where the keywords came from or how the script was invoked, then use _REQUEST.
1

the right way to do this is to define your data as variables in your mother file and then in your child file use those variables.
in the code you gave the parser looks for the file 'query.php?tblName=classes' and obviously it does not exist.

Comments

0

include/require both take a filename as a spec, not a URI. PHP doesn't parse it as a URI, so what you're trying won't work.

Better to set up an object that the included/required file can then inspect.

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.