Forum Moderators: coopster

Message Too Old, No Replies

Mysql value as parameter

Mysql value as parameter

         

photocroatia

12:36 pm on Nov 13, 2004 (gmt 0)

10+ Year Member



Hi All,

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

dreamcatcher

1:00 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

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.

:)

baertyp

1:51 pm on Nov 13, 2004 (gmt 0)

10+ Year Member



Photocroatia,

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;
?>

internally becomes

<?php
$var1 = "foo";
$var2 = "bar";
echo $var1 . $var2
?>

and will of course write
foobar
as 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...]

which points the browser to the given URL.

Regards
Markus

[edited by: baertyp at 1:56 pm (utc) on Nov. 13, 2004]

photocroatia

1:55 pm on Nov 13, 2004 (gmt 0)

10+ Year Member



I changed the code as per your instructions and did echo $row to see what it is returing,it echoed "Object" , this value does not exist in the table, am I still confused?

Thank you.

dreamcatcher

2:02 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you using the object operator?

$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 :)

photocroatia

2:06 pm on Nov 13, 2004 (gmt 0)

10+ Year Member



Baertyp,

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.

photocroatia

2:12 pm on Nov 13, 2004 (gmt 0)

10+ Year Member



Dreamcatcher

Thank you that worked!

$row = mysql_fetch_row($settings);
echo $row[0];

I then used:

@include ("http://www.mysite.com/body.php?myplace=$row[0]”);

Thank you very much.

dreamcatcher

2:34 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



welcome. :)

bobnew32

5:33 am on Nov 14, 2004 (gmt 0)

10+ Year Member



Hmmm just a question about the piece of code, what does putting the "@" in front of a line of code do? I see it sometimes at different connections and stuff. Thx.

jatar_k

5:44 am on Nov 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Error Control Operators [php.net]

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.

photocroatia

11:36 pm on Nov 14, 2004 (gmt 0)

10+ Year Member




I have hit an issue now, if somebody could help please.

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

dreamcatcher

1:38 am on Nov 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe try urlencode() [uk2.php.net].

@include ("http://www.mysite.com/body.php?myplace=urlencode($row[0])");

photocroatia

12:35 pm on Nov 15, 2004 (gmt 0)

10+ Year Member



Thank you, I tried urlencode() but it didn't work, I camr up with a work around, I moved my sql to the second script and just pased the ID to it, that worked fine.

Thanks again!

Slade

3:05 pm on Nov 15, 2004 (gmt 0)

10+ Year Member



This is for any of the guru's out there following this thread.

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.