bschultz Posted June 23, 2010 Share Posted June 23, 2010 I have a database that displays upcoming events and news stories. I'm trying to put an if statement in the database (text column) to only display events for a carnival that are happening today or later...not yesterday. Here's the code: eval('<?' . if (date('Y-m-d') <= '2010-06-30') {echo "<strong>Wednesday, June 30th</strong><br /><strong>5pm - 11pm</strong> Bingo Tent<br /><strong>5pm - 1am</strong> Entertainment Tent<br /><strong>6pm - midnight</strong> Mirriam's Midway (armband specials 6-11)<br /><strong>7p - 9p</strong> Bar Olympics<br /><strong>9p - 1a</strong> Live music with country band Heros and Thieves<br /><br />";} else { echo ""; } if (date('Y-m-d') <= '2010-07-01') {echo "<strong>Thursday, July 1st</strong><br /><strong>noon - 11pm</strong> Bingo Tent<br /><strong>noon - midnight</strong> Mirriam's Midway (armband specials 6-11)<br /><strong>5pm - 1am</strong> Entertainment Tent<br /><strong>9p - 1a</strong> Live music with Shirts and Skins<br /><br />";} else { echo ""; } . '?>'); That is also echoing the... ";} else { echo ""; }if (date('Y-m-d') <= '2010-07-01') {echo "Thursday, July 1st part of the code. How do I add an if statement to the database the right way? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/205643-eval-php-code-in-mysql-databse/ Share on other sites More sharing options...
bschultz Posted June 23, 2010 Author Share Posted June 23, 2010 If it makes it easier, I have the working if statement on a separate page. I can include the other file if need be. I tried <table><tr><td><img src="/shared/images/watercarnival.jpg" alt="fireworks" /> <br />The Jaycees are holding their 66th annual Water Carnival.<br /><br /> To get the Medallion Hunt clues sent to your cell phone each day, text the work HUNT to xxxx. <br /><br /> <?php eval("include '/home/public_html/evaltest.php';"); ?> </td></tr></table> but it didn't include the file. Quote Link to comment https://forums.phpfreaks.com/topic/205643-eval-php-code-in-mysql-databse/#findComment-1076139 Share on other sites More sharing options...
bschultz Posted June 23, 2010 Author Share Posted June 23, 2010 OK...I think I read the manual wrong. Can I put the eval into the database? Or does the eval have to go in the page that gets the info from the database? Quote Link to comment https://forums.phpfreaks.com/topic/205643-eval-php-code-in-mysql-databse/#findComment-1076143 Share on other sites More sharing options...
KevinM1 Posted June 23, 2010 Share Posted June 23, 2010 Wow, you're going about this in a completely wrong way. Are you storing the times of the events within the DB? If so, simply use a SELECT query to obtain all events occurring from today till the end of the carnival. Quote Link to comment https://forums.phpfreaks.com/topic/205643-eval-php-code-in-mysql-databse/#findComment-1076154 Share on other sites More sharing options...
bschultz Posted June 23, 2010 Author Share Posted June 23, 2010 I got it...I was reading that the eval went into the test of the database. Once I figured out that "regular" php code went into the database...and you eval the output...I got it figured out. Moral of the story...read things twice before starting to write the code! Quote Link to comment https://forums.phpfreaks.com/topic/205643-eval-php-code-in-mysql-databse/#findComment-1076159 Share on other sites More sharing options...
kenrbnsn Posted June 23, 2010 Share Posted June 23, 2010 That is a very bad way of doing things. Use the "select" statement to retrieve the data you want. Ken Quote Link to comment https://forums.phpfreaks.com/topic/205643-eval-php-code-in-mysql-databse/#findComment-1076160 Share on other sites More sharing options...
bschultz Posted June 23, 2010 Author Share Posted June 23, 2010 I did...like I said, once I realized that the eval code went on the page to print the database info...and not INSIDE the database...I got it working. <?php define('COMP_NAME', 'Mix 103.7'); $link = mysql_connect('localhost', 'xxxx', 'xxxx'); mysql_select_db('xxxx',$link); $sql = "SELECT * FROM content WHERE NOW() BETWEEN startdate AND enddate AND (station = 'mix' OR station = 'all') ORDER by RAND()"; $rs = mysql_query($sql,$link); $matches = 0; while ($row = mysql_fetch_assoc($rs)) { $matches++; echo "<tr><td><span class='itemheading2'>".str_replace('COMPANYNAMEHERE',COMP_NAME,stripslashes($row[headline]))."</span>"; echo "<span class='itemdetails2'><br /><br />"; $code = str_replace('COMPANYNAMEHERE',COMP_NAME,stripslashes($row[story])); eval('?>' . $code . '<?'); ///////// the above two lines were changed to make the old code execute the new database record that includes php scripting in the db echo "<br /><br /></span></td></tr>"; } if (! $matches) { echo (""); } echo ""; ?> The only php code inside the database is a php include Quote Link to comment https://forums.phpfreaks.com/topic/205643-eval-php-code-in-mysql-databse/#findComment-1076278 Share on other sites More sharing options...
KevinM1 Posted June 23, 2010 Share Posted June 23, 2010 That's still a horrible, horrible way of doing things. Potentially dangerous, too, if your db was ever compromised. Eval is hardly ever necessary. You should really look into changing your design. Quote Link to comment https://forums.phpfreaks.com/topic/205643-eval-php-code-in-mysql-databse/#findComment-1076289 Share on other sites More sharing options...
bschultz Posted June 24, 2010 Author Share Posted June 24, 2010 OK...here's the full story. I work for a group of radio stations (4 of them). Company events need to be placed on ALL 4 websites. Station specific contests and such only go on 1 website. I have the database set up like this: station (varchar) | startdate (datetime) | enddate (datetime)| headline (varchar) | story (longtext) The particular content that caused me to post today was a list of events for a 4th of July carnival. I wanted the list to only print events for today and in the future (several if statements...one for each day). Since the content of all four sites comes from one database...and I need dynamic content from that one database...how else can I do this? Quote Link to comment https://forums.phpfreaks.com/topic/205643-eval-php-code-in-mysql-databse/#findComment-1076323 Share on other sites More sharing options...
KevinM1 Posted June 24, 2010 Share Posted June 24, 2010 OK...here's the full story. I work for a group of radio stations (4 of them). Company events need to be placed on ALL 4 websites. Station specific contests and such only go on 1 website. I have the database set up like this: station (varchar) | startdate (datetime) | enddate (datetime)| headline (varchar) | story (longtext) The particular content that caused me to post today was a list of events for a 4th of July carnival. I wanted the list to only print events for today and in the future (several if statements...one for each day). Since the content of all four sites comes from one database...and I need dynamic content from that one database...how else can I do this? Just pull the content from the db? I mean, it looks like you're storing whole PHP files in the db, which I doubt is necessary. A 'story' implies text, which should be all you're storing. If you do need to dynamically access a PHP file, simply store its path in the db and use include to access it. Again, eval is hardly ever necessary. Quote Link to comment https://forums.phpfreaks.com/topic/205643-eval-php-code-in-mysql-databse/#findComment-1076564 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.