简体   繁体   中英

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.

 <?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. If everything it's okay, so use the second "SELECT". If it doesn't match, return nothing or return the first "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());
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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