在尝试从同一查询中的两个表中获取数据或用户名时,我遇到了一些问题。代码如下。那个代码有什么问题。我坐过了 UNION
, UNION ALL
等等,但仍然不起作用。通常通过放置一个表,查询显示结果,但是在放置两个表之后 $sql
is不显示任何结果。
$username = "";
$code = "";
$queryCondition = "";
if(!empty($_POST["search"])) {
foreach($_POST["search"] as $k=>$v){
if(!empty($v)) {
$queryCases = array("username","code");
if(in_array($k,$queryCases)) {
if(!empty($queryCondition)) {
$queryCondition .= " AND ";
} else {
$queryCondition .= " WHERE ";
}
}
switch($k) {
case "username":
$username = $v;
$queryCondition .= "username LIKE '%" . $v . "%'";
break;
case "code":
$code = $v;
$queryCondition .= "code LIKE '%" . $v . "%'";
break;
}
}
}
}
$orderby = " ORDER BY added desc";
$sql = "SELECT * FROM tableA OR SELECT * from tableB ORDER BY added" .
$queryCondition;
$href = 'abcd.php';
$perPage = 5;
$page = 1;
if(isset($_POST['page'])){
$page = $_POST['page'];
}
$start = ($page-1)*$perPage;
if($start < 0) $start = 0;
$query = $sql . $orderby . " limit " . $start . "," . $perPage;
$result = $db_handle->runQuery($query);
if(!empty($result)) {
$result["perpage"] = showperpage($sql, $perPage, $href);
}
?>
<div id="toys-grid">
<form name="frmSearch" method="post" action="list.php">
<div class="search-box">
<p><input type="text" placeholder="username" name="search[username]" class="demoInputBox" value="<?php echo $username; ?>" />
<input type="text" placeholder="Code" name="search[code]" class="demoInputBox" value="<?php echo $code; ?>" />
<input type="submit" name="go" class="btnSearch" value="Search">
<input type="reset" class="btnSearch" value="Reset" onclick="window.location='list.php'"></p>
</div>
<div class="not">
<?php
foreach($result as $k=>$v) {
if(is_numeric($k)) {
?>
<div class="" >
<div class="testimonial" >
<h4><a href="<?php echo $result[$k]["link"]; ?>"><?php echo $result[$k]["username"]; ?></a></h4>
<img src="<?php echo $result[$k]["image"]; ?>" style="width:100px;height:100px;">
<p style=""><?php echo $result[$k]["usertype"]; ?></p>
<div class="textads-link">
<strong>http://btcgenuis.com</strong><br>
<p><?php echo $result[$k]["regdate"]; ?></p>
</div>
</div>
</div>
<?php
}
}?>
</div><div style="clear: both; height: 10px"></div> <div style="text-align:right;margin:20px 0px 10px;">
<?php
if(isset($result["perpage"])) {
?>
<?php echo $result["perpage"]; ?>
<?php } ?>
</div>
</form>
希望能为这个错误找到一个最好的解决方案。
1条答案
按热度按时间osh3o9ms1#
UNION
几乎可以肯定是你想要的:这假设两个表具有相同的列。如果没有,你需要更换
SELECT *
使用特定列的列表,以便从两个表中选择相同的列。