0

我有 3 个表我想用我的搜索 php 访问。其中 2 个包含所有用户的个人资料信息。一种称为 cprofile,另一种称为 bprofile。它们都链接到包含用户名、电子邮件、密码和最重要的个人资料图片的用户表。

通过搜索引擎,我试图访问从搜索到个人配置文件表的信息,并从用户表中收集帐户信息。这是我搜索这两种类型的用户的代码:

$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'];
            }
        }
    }
}

有谁注意到我哪里出错了,我该如何解决这个问题?提前致谢

4

1 回答 1

0

我可能在这里走错了方向,但是您从查询中获取计数,然后在 while 循环中查找用户 ID。如果你想要两个然后做类似的事情

$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."%')
";
于 2013-03-18T02:32:29.367 回答