Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Mysql搜索功能_Php_Mysql - Fatal编程技术网

Php Mysql搜索功能

Php Mysql搜索功能,php,mysql,Php,Mysql,我正在尝试设置一个搜索功能,在这里我可以键入我想要的内容,也可以从下拉菜单中选择它,但是当字段为空时,我希望它显示所有内容 现在,当我搜索某个内容时,它会工作,但是当字段为空时,它根本不会显示任何内容。我使用prepare和bind_参数来设置mysql查询,这就是为什么设置起来很困难的原因,因为如果变量为空,我不知道如何轻松删除查询的该部分,以及如何更改绑定到查询的变量数量。这是一个问题 $stmt = $conn->prepare("SELECT * FROM re_tblcombin

我正在尝试设置一个搜索功能,在这里我可以键入我想要的内容,也可以从下拉菜单中选择它,但是当字段为空时,我希望它显示所有内容

现在,当我搜索某个内容时,它会工作,但是当字段为空时,它根本不会显示任何内容。我使用prepare和bind_参数来设置mysql查询,这就是为什么设置起来很困难的原因,因为如果变量为空,我不知道如何轻松删除查询的该部分,以及如何更改绑定到查询的变量数量。这是一个问题

$stmt = $conn->prepare("SELECT * FROM re_tblcombinationlist WHERE active != ? AND ModifierKey = ? AND (LootItem1Key = ? OR LootItem2Key = ? ) ORDER BY ReportedOn DESC LIMIT ? , ?");
$stmt->bind_param("isssii", $i = 0, $modifier, $lootsearch, $lootsearch, $limits, $max);
$recent1 = infoo($stmt);
我试图通过添加这个if-then语句来解决这个问题,但它没有改变任何东西

if(!isset($_POST["modifier"])){
$modifier = '';
}else{
$modifier = clean($_POST["modifier"]);
}

if(!isset($_POST["lootsearch"])){
$lootsearch = '';
}else{
$lootsearch = clean($_POST["lootsearch"]);
}
基本上,如果modifier或lootsearch是空的,我不希望该部分出现在mysql查询中,这很容易做到,但是这使得处理要绑定的变量数量非常困难,因此我试图找到一种方法,使其在变量为空时搜索所有内容


谢谢

我想我找到了解决您问题的方法,基于此:

首先创建此类:

class BindParam { 
    private $values = array();
    private $types = ''; 

    public function add( $type, $value ){ 
        $this->values[] = $value; 
        $this->types .= $type; 
    } 

    public function get() { 
        return array_merge(array($this->types), $this->values); 
    } 
}
在此之后,请确保在代码中导入该类,然后使用“在搜索文件中插入以下行”:

//User inputs, just for testing
$active = 0;
$modifier = 5;
$lootsearch = 'item';
$limits = 3;
$max = 5;

//Conditional binding
$bindParam = new BindParam(); 
$binds = array(); 
$binds[] = 'active = ?';
$bindParam->add('i', $active);

$query = "SELECT * FROM re_tblcombinationlist WHERE ";
if (!empty($modifier)) {
    $binds[] = 'ModifierKey = ?';
    $bindParam->add('s', $modifier);
} 
if (!empty($lootsearch)) {
    $binds[] = ' (LootItem1Key LIKE ? OR LootItem2Key LIKE ?) ';
    $bindParam->add('s', $lootsearch);
    $bindParam->add('s', $lootsearch);
}

$query .= implode(" AND ", $binds);
$query .= " ORDER BY ReportedOn DESC LIMIT ? , ?";

$bindParam->add('i', $limits);
$bindParam->add('i', $max);

//Uncomment this for debugging

//echo $query . '<br/>';

//Using the above user inputs query should be:
//SELECT * FROM re_tblcombinationlist WHERE active = ? AND ModifierKey = ? AND (LootItem1Key LIKE ? OR LootItem2Key LIKE ?) ORDER BY ReportedOn DESC LIMIT ? , ?

//var_dump($bindParam->get());

//Maybe you have to experiment a bit with those lines
$stmt = $conn->prepare($query);
$stmt->bind_param($bindParam->get());
$recent1 = infoo($stmt);
//用户输入,仅用于测试
$active=0;
$modifier=5;
$lootsearch='item';
$limits=3;
$max=5;
//条件绑定
$bindParam=新的bindParam();
$binds=array();
$binds[]=“active=?”;
$bindParam->add('i',$active);
$query=“从重新组合列表中选择*”;
如果(!空($modifier)){
$binds[]=“ModifierKey=?”;
$bindParam->add('s',$modifier);
} 
如果(!empty($lootsearch)){
$binds[]='(RootItem1Key-LIKE?或RootItem2Key-LIKE?);
$bindParam->add('s',$search);
$bindParam->add('s',$search);
}
$query.=内爆(“AND”,$binds);
$query.=“按描述限制报告的订单数?,?”;
$bindParam->add('i',$limits);
$bindParam->add('i',$max);
//取消对此的注释以进行调试
//回显$query。”
; //使用上述用户输入查询应为: //从re_tblcombinationlist中选择*其中active=?和ModifierKey=?和(ROOTITEM1KEY LIKE?或ROOTITEM2KEY LIKE?)按报告的订购说明限制? //变量转储($bindParam->get()); //也许你得试一下这些线条 $stmt=$conn->prepare($query); $stmt->bind_param($bindparm->get()); $recent1=infoo($stmt);
好吧,我想好了如何让它工作。首先,科斯塔斯所说的大部分都是解决方案,我不想因为他的工作而得到赞扬,但是需要稍微改变一下才能正常工作

首先,我添加了一个函数

function reference($arr){
$refs = array();
foreach($arr as $key => $value)
    $refs[$key] = &$arr[$key];
return $refs;

}
然后我改变了这个(来自Kostas的代码)


我选择的最终解决方案是将我的上一篇文章和Kostas的文章结合起来

首先,我们添加类:

class BindParam { 
private $values = array();
private $types = ''; 

public function add( $type, &$value ){ 
    $this->values[] = &$value; 
    $this->types .= $type; 
} 

public function get() { 
    $array = array_merge(array($this->types), $this->values);
    foreach($array as $key => $value)
    $refs[$key] = &$array[$key];
    return $refs; 
} 
}
然后,我们在需要动态使用查询的地方添加以下代码:

//User inputs, just for testing
$active = 0;
$modifier = 5;
$lootsearch = 'item';
$limits = 3;
$max = 5;

//Conditional binding
$bindParam = new BindParam(); 
$binds = array(); 
$binds[] = 'active = ?';
$bindParam->add('i', $active);

$query = "SELECT * FROM re_tblcombinationlist WHERE ";
if (!empty($modifier)) {
$binds[] = ' AND ModifierKey = ?';
$bindParam->add('s', $modifier);
} 
if (!empty($lootsearch)) {
$binds[] = ' AND (LootItem1Key LIKE ? OR LootItem2Key LIKE ?) ';
$bindParam->add('s', $lootsearch);
$bindParam->add('s', $lootsearch);
}

$query .= implode(" ", $binds);//removed the AND to allow for AND and OR
$query .= " ORDER BY ReportedOn DESC LIMIT ? , ?";

$bindParam->add('i', $limits);
$bindParam->add('i', $max);

//Uncomment this for debugging

//echo $query . '<br/>';

//Using the above user inputs query should be:
//SELECT * FROM re_tblcombinationlist WHERE active = ? AND ModifierKey = ? AND     (LootItem1Key LIKE ? OR LootItem2Key LIKE ?) ORDER BY ReportedOn DESC LIMIT ? ,     ?

//var_dump($bindParam->get());

//Maybe you have to experiment a bit with those lines
$stmt = $conn->prepare($query);
call_user_func_array(array($stmt, 'bind_param'), $bindParam->get());//Required in order to use the array given by bindParam->get()
$recent1 = infoo($stmt);
//用户输入,仅用于测试
$active=0;
$modifier=5;
$lootsearch='item';
$limits=3;
$max=5;
//条件绑定
$bindParam=新的bindParam();
$binds=array();
$binds[]=“active=?”;
$bindParam->add('i',$active);
$query=“从重新组合列表中选择*”;
如果(!空($modifier)){
$binds[]='和ModifierKey=?';
$bindParam->add('s',$modifier);
} 
如果(!empty($lootsearch)){
$binds[]='和(RootItem1Key-LIKE?或RootItem2Key-LIKE?);
$bindParam->add('s',$search);
$bindParam->add('s',$search);
}
$query.=内爆(“,$binds”)//已删除和,以允许和或
$query.=“按描述限制报告的订单数?,?”;
$bindParam->add('i',$limits);
$bindParam->add('i',$max);
//取消对此的注释以进行调试
//回显$query。”
; //使用上述用户输入查询应为: //从re_tblcombinationlist中选择*其中active=?和ModifierKey=?和(ROOTITEM1KEY LIKE?或ROOTITEM2KEY LIKE?)按报告的订购说明限制? //变量转储($bindParam->get()); //也许你得试一下这些线条 $stmt=$conn->prepare($query); 调用_user_func_数组(数组($stmt,'bind_param'),$bindParam->get())//使用bindParam->get()给定的数组时需要 $recent1=infoo($stmt);
您需要实际编写一些逻辑代码来更改语句。你做到了吗?你可以使用
一样作为比较运算符,然后简单地使用
%
作为任何空字段的“搜索值”…我有一个if-then语句,它将变量设置为“and ModifierKey=?”如果设置了搜索,我将该变量放入查询中,问题是如何更改bind参数,因为可以设置1个搜索字段,也可以同时设置这两个字段。我会尝试使用LIKEOk,我尝试了LIKE语句,我得到了更好的结果,但是我无法得到我需要的全部结果。如果我这样做查询:“$stmt=$conn->prepare”(“从活动的re_tblcombinationlist中选择*!=?和(LootItem1Key-like?或LootItem2Key-like?)ORDER BY ReportedOn DESC LIMIT?,?”)它会工作,如果我这样做:“$stmt=$conn->prepare”(“从活动的re_tblcombinationlist中选择*!=?和ModifierKey-like?ORDER BY ReportedOn DESC LIMIT?,?”)它会起作用,但如果我把它们结合起来,它就不起作用了。看起来它会很好地起作用。我想出了一个临时的解决办法,但它更像是绷带。我会尝试一下,看看会发生什么。我试过这个,它在一定程度上起作用。我让代码保持原样,因为$bindParam->get()返回一个数组,所以它不起作用。我试图将其内爆,但这混淆了bind+\u param函数,因为从技术上讲,此时它只有1个字符串。但是,如果我将其设置为一个变量,例如$array,那么我可以使用$array[0]、$array[1]、。。。然而,这并没有真正起作用,因为它是由一个静态的变量数来设置的,而不是像我们试图实现的那样由一个动态的数来设置的。同样,如果你使用PDO,也许你
class BindParam { 
private $values = array();
private $types = ''; 

public function add( $type, &$value ){ 
    $this->values[] = &$value; 
    $this->types .= $type; 
} 

public function get() { 
    $array = array_merge(array($this->types), $this->values);
    foreach($array as $key => $value)
    $refs[$key] = &$array[$key];
    return $refs; 
} 
}
//User inputs, just for testing
$active = 0;
$modifier = 5;
$lootsearch = 'item';
$limits = 3;
$max = 5;

//Conditional binding
$bindParam = new BindParam(); 
$binds = array(); 
$binds[] = 'active = ?';
$bindParam->add('i', $active);

$query = "SELECT * FROM re_tblcombinationlist WHERE ";
if (!empty($modifier)) {
$binds[] = ' AND ModifierKey = ?';
$bindParam->add('s', $modifier);
} 
if (!empty($lootsearch)) {
$binds[] = ' AND (LootItem1Key LIKE ? OR LootItem2Key LIKE ?) ';
$bindParam->add('s', $lootsearch);
$bindParam->add('s', $lootsearch);
}

$query .= implode(" ", $binds);//removed the AND to allow for AND and OR
$query .= " ORDER BY ReportedOn DESC LIMIT ? , ?";

$bindParam->add('i', $limits);
$bindParam->add('i', $max);

//Uncomment this for debugging

//echo $query . '<br/>';

//Using the above user inputs query should be:
//SELECT * FROM re_tblcombinationlist WHERE active = ? AND ModifierKey = ? AND     (LootItem1Key LIKE ? OR LootItem2Key LIKE ?) ORDER BY ReportedOn DESC LIMIT ? ,     ?

//var_dump($bindParam->get());

//Maybe you have to experiment a bit with those lines
$stmt = $conn->prepare($query);
call_user_func_array(array($stmt, 'bind_param'), $bindParam->get());//Required in order to use the array given by bindParam->get()
$recent1 = infoo($stmt);