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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘