简体   繁体   中英

I have a mysql query in my php code that is cut and pasted in two places. How can I get it to be in one place?

I have a query in index.php and the exact same query in update.php. How can I have the query in a single location so it's easy to manage?

This is my query:

$sets=$_GET["sets"];
$sets = "'" . str_replace(array("'", ","), array("\\'", "','"), $sets) . "'";

// test code
$query = " 
    SELECT 
        *
    FROM
        `cards`
    WHERE
        `setName` in ($sets)
    ORDER BY
        `setName` DESC,
    LIMIT
        500
";

Include a header.php , or a functions.php . Then, define the queries within the file. You can then access them, after including them.

require 'header.php';
    OR
include 'header.php';



function make_query($val)
{
    $res = "SELECT * FROM users WHERE id = " . $val;
    return $res;
}

创建第三个文件query.php ,将查询放入此文件中(可能在变量中),并同时包含index.phpupdate.php的文件。

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