Forum Moderators: coopster
I am very new to php and mysql.
I have the following query which works fine
$settings = mysql_query("SELECT * FROM table_name WHERE column_id = $var_no") or die("query failed");
$settings = mysql_fetch_array($settings);
echo $settings["description"]
-----------
The problem I have is I want to use that value that is returned eg. echo $settings["description"] as a parameter when calling another php script
@include ("http://www.mysite.com/body.php?myplace=NEW_VARIABLE_RETURNED");
NEW_VARIABLE_RETURNED is where the parameter should go I tried a lot of options without success.
Thank you
Firstly, if you are only pulling one row of data, mysql_fetch_row or fetch_object are better. Always put your query between apostrophes. Also, best not to use a variable name as one you are already using. This can cause problems.
Try:
$settings = mysql_query("SELECT * FROM table_name WHERE column_id = '$var_no' LIMIT 1") or die("query failed");
$row = mysql_fetch_object($settings);
@include ("http://www.mysite.com/body.php?myplace=" . $row->description);
This should work ok. Just remember to use the include after you have pulled the data. I have also added the LIMIT clause, which is a good idea if you are only pulling one row.
:)
by include()ing a file the parser replaces the include() statement with the contents of the referenced file.
An example:
<?php
// script1.php
$var1 = "foo";
$var2 = "bar";
include("script2.php");
?>
<?php
// script2.php
echo $var1 . $var2;
?>
<?php
$var1 = "foo";
$var2 = "bar";
echo $var1 . $var2
?>
foobaras naturally all of the variables defined in script1.php are also valid for script2.php.
What you are looking for most probably is:
// query goes here, result in $result
header("Location: [some.whe.re...]
Regards
Markus
[edited by: baertyp at 1:56 pm (utc) on Nov. 13, 2004]
$row = mysql_fetch_object($settings);
echo $row->description;
Should work fine if the description row exists. Alternatively, you can try mysql_fetch_row.
$row = mysql_fetch_row($settings);
echo $row[0];
This assumes that the description row is your first row. If not, change the numerical indice.
dc :)
The variable bit I understand and passing that to the calling php script.
In my original example when I echo the value eg.
echo $settings["description"]
This displays the value I want but I cant pass that to the calling php eg.
@include ("http://www.mysite.com/body.php?myplace=$settings["description"]");
I also can’t seem to assign that to a variable eg.
$newval=$settings["description"]
and then pass that to the calling script:
@include ("http://www.mysite.com/body.php?myplace=$newval");
Hopefully it is a little clearer now, sorry its hard to explain.
PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
The issue is when $row[0] is 2 words, if I echo $row[0] both words are displayed, but when I call my other script:
@include ("http://www.mysite.com/body.php?myplace=$row[0]”);
It doesn't work, doesn't run, I have tried single quoates in various places but no luck.
Any assistance is appreciated.
Thank you
@include ("http://www.mysite.com/body.php?myplace=urlencode($row[0])");
Doesn't include'ing from an http add an extra level of indirection and processing to the execution of a page?
It appears the OP isn't trying to do a redirect, or I would have expected them to latch onto the header(...) example that was posted.
How about just wrapping the code in the include file into a function, and calling it like:
include('body.php');
doBody($row[0]); At least then, if the code was needed more than once, you wouldn't have to call the page again, you'd just doBody() with the next variable.