简体   繁体   English

PHP搜索MySQL数据库

[英]Php search mysql database

Sorry if I'm asking an already answered question, but I was not able to find my specific set-up. 抱歉,我要问一个已经回答的问题,但是找不到我的特定设置。

I have a php file called functions.php, where I store all my functions and where I prepare my database. 我有一个名为functions.php的php文件,其中存储了所有函数并准备了数据库。 A small example would be as follows: 一个小例子如下:

function getUsers()
{
        global $mysqli,$db_table_prefix;
        $stmt = $mysqli->prepare("SELECT
                id,
                user_name,
                FROM db_users");
        $stmt->execute();
        $stmt->bind_result($id, $user);
        while ($stmt->fetch()){
                $row[] = array('id' => $id, 'user_name' => $user);
        }
        $stmt->close();
        return ($row);
}

I want to make a search function, with a form where I can search with OPTION VALUES, ie values as: id , user_name . 我想做一个搜索功能,用可以在其中使用OPTION VALUES进行搜索的表格,即iduser_name值。

By having functions.php I am already establishing the connection to the mysql database, How can I search the database with my '%searchrequest%' with a search.php? 通过使用functions.php,我已经建立了与mysql数据库的连接,如何使用search.php的'%searchrequest%'搜索数据库?

Or do you suggest another solution? 还是您建议其他解决方案?

Just pass these search values into your current function. 只需将这些搜索值传递到当前函数即可。 If nothing is passed,it will fetch all the data. 如果没有传递任何内容,它将获取所有数据。 If there is any value passed it will filter based on it. 如果传递了任何值,它将根据该值进行过滤。

function getUsers($id="",$user_name="")
{
        global $mysqli,$db_table_prefix;

        $query = "SELECT
                id,
                user_name,
                FROM db_users WHERE 1 = 1 ";
        if(trim($id)) 
           $query .= " AND id = '".$id."'"; 

        if(trim($user_name)) 
           $query .= " AND user_name = '".$user_name."'"; 


        $stmt = $mysqli->prepare($query);


        $stmt->execute();
        $stmt->bind_result($id, $user);
        while ($stmt->fetch()){
                $row[] = array('id' => $id, 'user_name' => $user);
        }
        $stmt->close();
        return ($row);
}

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

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