Jump to content


Photo

php in a mysql database


  • Please log in to reply
13 replies to this topic

#1 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 20 October 2006 - 01:26 PM

i want to be able to update links quickly and easily so id only have to change 1 page, (well 1 row of a database)

so i added code to all pages saying to fetch this row of a database and it won't read any php inside it :(

it will read html links but not the '<? echo "$mem[id]"; ?>' etc... any ideas? it will make my life so much simpler :D
Visit my Game - Under Construction

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 October 2006 - 01:31 PM

when you store PHP within a database and pull it out, you may need to run the eval() function on the string. this then evaluates the PHP in the string and runs that code.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 20 October 2006 - 01:34 PM

never done the eval() func before ???

something like:
<?
eval('$qlink[quicklinks]');
echo "$qlink[quicklinks]";
?>

??
Visit my Game - Under Construction

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 October 2006 - 01:40 PM

maybe you should show us what your $qlink['quicklinks'] might hold. eval() is a very touchy function, and should be used sparingly, so let's break it down a little further, and i'll see if i can help you with an optimal solution. i'll need a little more information, though:

1) what is the code in the database supposed to accomplish?

2) how are you using the PHP code you pull from the database on the page?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 20 October 2006 - 01:46 PM

it pulls out code for the quicklinks eg.
<a href="blah.html">link</a><----that comes out fine
<a href="link.php?<? echo "$mem[id]"; ?>">link2</a> <----that doesnt...


so basically i add :
<?
echo "qlink[quicklinks]";
?>
to every page and then i only have to edit the database when i change things...

i think that answers both questions???



Visit my Game - Under Construction

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 October 2006 - 02:02 PM

hmm... seems like you'd be better off using a sort of templating scheme (or else changing how you store the code in the DB). here's what i would suggest, in order of preference:
<?php
// first choice:
// create a template for your ID in the database:
// database entry: <a href="link.php?{MEM_ID}">link</a>
$qlink['quicklinks'] = preg_replace('|\{MEM_ID\}|', $mem['id'], $qlink['quicklinks']);
echo $qlink['quicklinks'];
?>

basically, this option creates a template tag "{MEM_ID}" that would be in your quicklink in the database, and you would simply replace that tag with your query result.

option 2:
<?php
// database entry: echo "<a href=\"link.php?$mem[id]\">link</a>";
eval($qlink['quicklinks']);
?>

notice how i actually would record the entire link as one echo statement in the database. this would allow me to run eval on the statement and output my result.

does this help some?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 20 October 2006 - 02:29 PM

what if i have 2 different php bits
$mem[id] and $mem[username]
would you change it to:
$qlink['quicklinks'] = preg_replace('|\{MEM_ID\}|', $mem['id'],  '|\{MEM_USERNAME}\|', $mem['username'], $qlink['quicklinks']);
Visit my Game - Under Construction

#8 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 October 2006 - 02:35 PM

preg_replace() takes arrays as the arguments for the changes, so you'd do something like this:
<?php
$tags = array('|\{MEM_ID\}|', '\{MEM_USERNAME\}|');
$vals = array($mem['id'], $mem['username']);
$qlink['quicklinks'] = preg_replace($tags, $vals, $qlink['quicklinks']);
?>

make sense?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#9 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 20 October 2006 - 02:45 PM

right now it shows all info plus {} around those bits we changed
Visit my Game - Under Construction

#10 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 October 2006 - 02:50 PM

right now it shows all info plus {} around those bits we changed


??? i don't follow. can you post the string before and after the change? basically, do this:
<?php
echo "Before: $qlink[quicklinks]<br /><br />\n";
$qlink['quicklinks'] = preg_replace($tags, $vals, $qlink['quicklinks']);
echo "After: $qlink[quicklinks]<br />\n";
?>

this way, i can see exactly what's being changed.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#11 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 20 October 2006 - 02:59 PM

Before: {MEM_USERNAME}'s House
Iridia Central
My Clutch
Mine Shaft
Portal
Battle
Arena
Message Board
My Account
Logout


After: {Lord of the Abyss}'s House
Iridia Central
My Clutch
Mine Shaft
Portal
Battle
Arena
Message Board
My Account
Logout

Visit my Game - Under Construction

#12 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 October 2006 - 03:10 PM

that is strange... can you post your code for your preg_replace()? just want to double check your syntax on the patterns. if you'd rather, you could probably use str_replace() a little more easily:
<?php
$tags = array('{MEM_ID}', '{MEM_USERNAME}');
$vals = array($mem['id'], $mem['username']);
$qlink['quicklinks'] = str_replace($tags, $vals, $qlink['quicklinks']);
?>

does that help?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#13 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 20 October 2006 - 03:13 PM

yay the str replace worked :D

my code before with preg :)
$tags = array('{MEM_ID}', '{MEM_USERNAME}', '{MEM_CLUTCH}');
$vals = array($mem['id'], $mem['username'], $mem[clutch]);
$qlink['quicklinks'] = preg_replace($tags, $vals, $qlink['quicklinks']);

thanks for the help :D
Visit my Game - Under Construction

#14 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 October 2006 - 03:18 PM

just for reference, the reason your code with preg_match() wasn't working is because you needed to escape the brackets to get them to match:
<?php
$tags = array('|\{MEM_ID\}|', '|\{MEM_USERNAME\}|', '\{MEM_CLUTCH\}|');
?>

glad you got it working!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users