简体   繁体   中英

php search engine

I have 3 tables i want to access with my search php. 2 of them contain all the users profile information. One is called cprofile and the other is bprofile. they are both linked to a user table that contains the username, email, password and most importantly the profile picture.

With the search engine i am trying to access both the info from the search to the individual profile tables and to gather the account information from the user table. This is my code for searching for both types of users:

$search_output = "";
if( isset($_POST['searchquery']) && $_POST['searchquery'] != "" ) {
    $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
    if($_POST['filter1'] == "Whole Site") {
        $sqlCommand = "(SELECT count(*) FROM cprofile WHERE firstname ='%".$searchquery."%' OR lastname ='%".$searchquery."%')
            UNION (SELECT  count(*) FROM bprofile WHERE cname ='%".$searchquery."%')";
        $query = mysqli_query( $db_conx, $sqlCommand ) or die( mysqli_error($db_conx) );
        $count = mysqli_num_rows( $query ); 
        while( $row = mysqli_fetch_array($query, MYSQLI_ASSOC) ) {
            $userid = $row["userID"];
            $ret = mysqli_query($db_conx, "SELECT id, username FROM users WHERE id='$userid'");
            while($raw = mysqli_fetch_array($ret, MYSQLI_ASSOC)) {
                $username = $raw['username']; $file= $raw['avatar'];
            }
        }
    }
}

Does anyone notice where i am going wrong and how i could resolve this issue? Thanks in advance

I might be going in the wrong direction here but you are getting counts from your query and then looking for a userID in the while loop. If you want both then do something like

$sqlCommand = "
  (SELECT userid, count(*) as count FROM cprofile WHERE firstname ='%".$searchquery."%' OR lastname ='%".$searchquery."%')
    UNION 
  (SELECT userid, count(*) as count FROM bprofile WHERE cname ='%".$searchquery."%')
";

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