首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从mysql查询创建函数

从mysql查询创建函数
EN

Stack Overflow用户
提问于 2018-01-26 19:32:52
回答 1查看 29关注 0票数 0

我有一个特定的查询,它正在从我的数据库中读取5篇最新的文章类别(mysql专栏)“运动”

我想运行多个查询,一个用于运动,一个用于汽车,一个用于书籍等,而不是多次重写完整的查询,并用汽车/书籍等替换类别sport,我想知道是否有可能将其重建为一个函数,在该函数中我输入类别(如第2行中的体育),它输出一个字符串$sports (如第10/11行),所有从数据库中检索的内容都在查询中定义。

代码语言:javascript
复制
// Build feed source for the latest News of Sport
$sqlCommand = "SELECT * FROM feeds where category like 'Sport' ORDER BY id DESC LIMIT 5"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 

$sport = '';
while ($row = mysqli_fetch_array($query)) { 
    $fid1 = $row["id"];
    $title1 = $row["title"];
    $description1 = $row["description"];
    $sport .= '<h2><b><a href="detail/' . $fid1 . '/' . $title1 . '" title="' . $title1 . '">' . $title1 . '</a></b></h2>';
    $sport .= '<a href="detail/' . $fid1 . '/' . $title1 . '" title="' . $title1 . '">' . $description1 . '</a><br/>';

} 
mysqli_free_result($query);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-02 23:31:27

这是我最终解决这个问题的方法:

代码语言:javascript
复制
function nieuws ($category,$color) {

    global $myConnection;
    $sqlCommand = "SELECT * FROM feeds where category like '$category' ORDER BY id DESC LIMIT 1"; 
    $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
    $output = '';
    $output = "<div class=headlines style=background:$color><a class=newslink href='$category'>$category</a></div>";
    while ($row = mysqli_fetch_array($query)) { 
        $fid1 = $row["id"];
        $title1 = $row["title"];
        $titleseo1 = seofy($title1);
        $description1 = $row["description"];
        $photo1 = $row["photo"];
        if ('' == $photo1) {$photo1='images/pic1.jpg';};
        $output .= "<div class=news2 style=background:#F0F8FF><img src='$photo1' alt='photo' style='width:200px;height:150px;object-fit:cover;border:0;margin:0px 5px 0px 0px;float:left'>"; //object-fit: cover zorgt dat het origineel het plaatje vult zonder distortion, is in principe cropping
        $output .= '<b><a href="detail/' . $fid1 . '/' . $titleseo1 . '" title="' . $title1 . '">' . $title1 . '</a></b></br>';
        $output .= '<a href="detail/' . $fid1 . '/' . $titleseo1 . '" title="' . $title1 . '">' . $description1 . '</a><br/>';
        $output .= "</div>";
        } 
        mysqli_free_result($query);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48460548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档