dow98764 2014-07-23 23:13
浏览 86
已采纳

如何从MySQL数据库表中调用PHP函数以使该函数正常运行?

This is a really tough question to ask. He is my issue/question.

Is it possible to put a PHP function inside a MySQL table/cell and have the function run normally when called via a PHP query. If so How can I do it?

Here is my query code:

$recent_query = "SELECT * FROM `recently-added` ORDER BY `created` DESC LIMIT 4";

if(!$recent_query_result = $con->query($recent_query)){
    die('There was an error running the query [' . $con->error . ']');

}

Here is my loop code that displays the results of the query:

while($row = $recent_query_result->fetch_assoc()) {
  echo  '<div class="col-md-3 col-sm-6 md-margin-bottom-40">
        <div class="easy-block-v2">
            <div class="easy-bg-v2 rgba-' . $row['color'] . '">New</div>
            <img class="img-bordered" alt="' . $row['image-alt'] . '" src="' . $row['image'] . '">       
            <h3><i class="fa fa-smile-o"></i> ' . $row['site-name'] . '</h3>
            <!-- Begin Recently Added Footer Description -->
        </div>  
    </div>';
}

In my database and inside the table 'recently-added' I have a column named 'site-name'. I iterate through the loop and display the results onto my page. In the site name cell I put my PHP function for each record. That function is:

<?php star5(); ?>

When I refresh the page and look at where the function should be called it's a blank space. Nothing there. When I go to look at the source code to see if there is an error it shows me the exact characters that I put into the cell. It places the text onto the page and the server does not run the script or interpret the function.

Here is an image of the table in the database. This is in regard to te column 'site-name':

http://i.imgur.com/0XWBqtR.jpg

Been trying to fix this for almost 4 hours now. As always thanks in advance!

EDIT: Here is the function it's self

function star5() {

echo '<li><i class="color-green fa fa-star"></i></li>
      <li><i class="color-green fa fa-star"></i></li>
      <li><i class="color-green fa fa-star"></i></li>
      <li><i class="color-green fa fa-star"></i></li>
      <li><i class="color-green fa fa-star"></i></li>';
}
  • 写回答

3条回答 默认 最新

  • doudi2833 2014-07-23 23:35
    关注

    Not the best way, but as in the current situation, it's the solution which is most close to the good practices for introducing flags in DB in order to have logic over it.

    Imagine you want to control if a user is administrator or not. You can add a column is_admin with 1 or 0 values, then while fetching the results you can check if ($row['is_admin'] == 1) { // proccess the logic for administrator } else { // process logic for normal user }

    This could apply here, as you want to execute backend logic on certain event, or just show the row contents, if the event is not present.

    According to be it will be one more level better if you introduce new column for this, and check it in order to know if you should call star5() or not, but in the current context you can add a string, on which presentation you will trigger the function.

    Let's say the string is CUSTOM FUNCTION. Then, if you want to execute star5() upon fetch pointer moves on this row, then you add CUSTOM FUNCTION string in this row. Otherwise you add the normal content (e.g. real sitename).

    Afterwards, when you fetch the records, you perform a simple condition check:

    if ($row['sitename'] == 'CUSTOM FUNCTION') {
        star5();
    } else {
        echo $row['sitename'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值