简体   繁体   English

PHP_MYSQL-根据特定文件在两个查询之间进行选择

[英]PHP_MYSQL - Select between two query, according to a specific file

I wanna make a function who can make a select. 我想做一个可以选择的功能。 But the problem is the first condition is always true. 但是问题是,第一个条件始终是正确的。 So I want to switch between this two select, according to the selected file. 因此,我想根据所选文件在这两个选择之间切换。

<?php
function select_cat(){
    if (file('index_cms.php')){
        return "SELECT * FROM posts_anon";
    } elseif(file('category.php')){
        return "SELECT * FROM posts_anon WHERE category = '$_GET[cat_name]'";
    }
    return 1;
}

select_cat();
$run_side = mysqli_query($connexion, select_cat());
?>

Maybe someone can help me to fix that. 也许有人可以帮我解决这个问题。 Thx. 谢谢。

Update : 更新:

Okay I found the solution, based on the post of Mazhar Hussain. 好的,我根据Mazhar Hussain的帖子找到了解决方案。

 <?php function select_cat() { if (isset($_GET['cat_name'])) { if ($_GET['cat_name'] == true && file('category.php')) { return "SELECT * FROM posts_anon WHERE category = '$_GET[cat_name]'"; } else { return ''; } } else { return "SELECT * FROM posts_anon"; } } select_cat(); $run_side = mysqli_query($connexion, select_cat()); ?> 

I've changed the order. 我改变了顺序。 Firstly, I verify if there is something in $GET_['cat_name'], and if it matches with the file category.php. 首先,我验证$ GET _ ['cat_name']中是否有内容,以及是否与文件category.php相匹配。 If everything it's okay, so use the second "SELECT". 如果一切正常,请使用第二个“ SELECT”。 If it doesn't match, return nothing or return the first "SELECT". 如果不匹配,则不返回任何内容或返回第一个“ SELECT”。

Thx everyone. 谢谢大家。

Basically what you are looking for is the script name currently being executed by server use some thing like below 基本上,您要查找的是服务器当前正在执行的脚本名称,请使用如下所示的内容

<?php
function select_cat()
{
    $fname = basename(__FILE__, '.php');
    if ($fname == 'index_cms.php'){
        return "SELECT * FROM posts_anon";
    } elseif($fname == 'category.php'){
        return "SELECT * FROM posts_anon WHERE category = '$_GET[cat_name]'";
    }
    return 1;
}

select_cat();
$run_side = mysqli_query($connexion, select_cat());
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM