Jump to content

[SOLVED] Print PHP Variable from Database field and have it work.


crimsonmoon

Recommended Posts

I'm printer a url with php in it from a database field and all that's printing is the link and the variable code. The variable is not changing to the actual #.

 

For example this is what I'm using.

 

This in in the DB.

 

http://link.com/home.php?x=$variable

 

I use this print "$DBField"; and this prints.

 

http://link.com/home.php?x=$variable

 

The variable in not changing over to it's value when it = 1 for instance.

 

Any suggestions?

Link to comment
Share on other sites

I know how variable work. What I'm asking is if I have a database field that hold a link like I posted above with a variable in it.

 

Then I print the link out in a page right now it just print the variable like $variable.

 

It's not converting the variable to it's actual value.

 

How do you get a variable to change it it's value when the variable in within a variable.

Link to comment
Share on other sites

This is the field in the DB (It's a link) : http://site.com/home.php?id=$x

 

Now we're on the php page.

 

at the top of the page

 

$x = 23;

 

We pull the field into $field[dblink].

 

So $field[dblink] is the link I have above.

 

in the php page I print:

 

print "<a href=$field[dblink]>Click Here</a>";

 

instead of <a href=http://site.com/home.php?id=23>Click Here</a>

 

I get <a href=http://site.com/home.php?id=$x>Click Here</a>

 

So the $x is not changing over to 23 .....what am I doing wrong?

Link to comment
Share on other sites

can you post the results of this please so i can see what your working with

 

<?php
echo "<br />";
var_dump($field);
echo "<br />";
print_r($field);
?>

 

 

as a note i tried this and it worked on my site

 

basically i createds a array (same as i would get a return from a database) and printed it

 

<?php

$field = array('dblink' => 10);
print "<a href=text2.php?x=$field[dblink]>Click Here</a>";
?>

Link to comment
Share on other sites

 

string(74) "http://www.toprpgames.com/vote.php?idno=437&field1=$var[id]&field2=$var2"

http://www.toprpgames.com/vote.php?idno=437&field1=$var[id]&field2=$var2

 

when i try doing a $db['field'] i get the error about encapsulated white space

 

[Wed Feb 28 21:54:18 2007] [error] PHP Parse error:  syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

Link to comment
Share on other sites

That is different. What the OP has is a string that has the value of

'http://site.com/home.php?id=$x'

At some later point after setting the string's value, the OP sets the variable $x to 23.

 

What is needed is to use the eval() function:

<?php
$str = 'http://site.com/home.php?id=$x';
$x = 23;
echo $str;
eval("\$str2 = \"$str\";");
echo '<br>'.$str2;
?>

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.