PHP 正在搜索名字和姓氏

PHP Searching with first and last name

我和我的项目合作伙伴对 PHP 比较陌生,需要一些帮助来搜索多个列。我们正在尝试建立一个正常运行的搜索栏,根据搜索到的名字和姓氏,returns 喜剧演员的名字和相应的 youtube url。我们让它可以与正在搜索的名字或姓氏一起使用,但不能同时与它们一起使用。 (即 Sarah 或 Silverman 可以工作,但 Sarah Silverman 不行)我们尝试将子句更改为 AND 但这根本不起作用。下面是我们的搜索代码。 行

$searchdb = mysqli_query($con,"SELECT * 
                                FROM comedian 
                                WHERE fname LIKE '%$searchq%' 
                                OR lname LIKE '%$searchq%'") 
        or die ("Could not search.");

我们正在努力 modify.Thank 你!

 <?php
session_start();

$con = mysqli_connect('localhost','root','') or die("Could not connect");

mysqli_select_db($con, 'youtube') or die(mysqli_error($con));
$output = '';

if(isset($_POST['search'])){
    $searchq = $_POST['search'];
    //Will replace everything that is not a letter or number with blank.
    $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

    $searchdb = mysqli_query($con,"SELECT * 
                                    FROM comedian 
                                    WHERE fname LIKE '%$searchq%' 
                                    OR lname LIKE '%$searchq%'") 
            or die ("Could not search.");
    $count = mysqli_num_rows($searchdb);

    if($count == 0){
        $output = 'There were no search results.';
        print("$output");
    }else{
        while($row = mysqli_fetch_array($searchdb)){
            //Variables
            $fname = $row['fname'];
            $lname = $row['lname'];
            $comedianid = $row['comedianid'];

            //Output first and last name
            $output .= '<div> <b>Comedian:</b> '.$fname.' '.$lname.'</div>';



            //Matching comedian with respective video URL using about table relation.
            $searchdb2 = mysqli_query($con,"SELECT * FROM about WHERE comedianid LIKE '%$comedianid%'") or die ("Could not find a video by this author.");
            //Fetches comedian id from about table
            $row2 = mysqli_fetch_array($searchdb2);
            $url = $row2['url'];

            $outputvid = preg_replace("#.*youtube\.com/watch\?v=#","",$url);
            $outputvid = '<iframe width="700" height="600" src="https://www.youtube.com/embed/'.$outputvid.'"></iframe>';


            print("$output");   
            print("$outputvid");

            $output = '';
        }



    }

}

您可以连接名字和姓氏,然后根据该字符串进行搜索...

SELECT * 
FROM comedian 
WHERE fname ||  ' ' || lname LIKE '%Sarah Silverman%'

这将显示 "Sarah Silverman",即使 Sarah 存储在 fname 而 Silverman 存储在 lname

编辑: 我已经在SQLite中测试过了,你可能需要根据你的数据库管理系统将||更改为+