Jump to content

php in a mysql database


almightyegg

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/24542-php-in-a-mysql-database/
Share on other sites

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


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:
[code]
<?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'];
?>
[/code]

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:
[code]
<?php
// database entry: echo "<a href=\"link.php?$mem[id]\">link</a>";
eval($qlink['quicklinks']);
?>
[/code]

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

make sense?
[quote author=almightyegg link=topic=112118.msg454967#msg454967 date=1161355540]
right now it shows all info plus {} around those bits we changed
[/quote]

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

this way, i can see exactly what's being changed.
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:
[code]
<?php
$tags = array('{MEM_ID}', '{MEM_USERNAME}');
$vals = array($mem['id'], $mem['username']);
$qlink['quicklinks'] = str_replace($tags, $vals, $qlink['quicklinks']);
?>
[/code]

does that help?
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
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:
[code]
<?php
$tags = array('|\{MEM_ID\}|', '|\{MEM_USERNAME\}|', '\{MEM_CLUTCH\}|');
?>
[/code]

glad you got it working!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.