未设置绑定参数的数据时的PHP PDO

未设置绑定参数的数据时的PHP PDO,php,pdo,Php,Pdo,我从下面这样的查询字符串中获取数据,其中所有3个国家、州和子都可能未设置 有时可能是 http://www.example.com/index.php?country=US&state=california&sub=sanjose http://www.example.com/index.php?country=US&state=california http://www.example.com/index.php?country=US 那么我会: $stmt = $conn->prepa

我从下面这样的查询字符串中获取数据,其中所有3个
国家、州和子
都可能未设置

有时可能是

http://www.example.com/index.php?country=US&state=california&sub=sanjose

http://www.example.com/index.php?country=US&state=california

http://www.example.com/index.php?country=US

那么我会:

$stmt = $conn->prepare('select username from arraytest where country = :country and state = :state and sub = :sub');
    $stmt->bindParam(':country', $_GET['country']);
    $stmt->bindParam(':state', $_GET['state']);
    $stmt->bindParam(':sub', $_GET['sub'])
    $stmt->execute();

    while($rows = $stmt->fetch()) {
        echo $rows['username'];
        echo '<br>';
    }
$stmt=$conn->prepare('从arraytest中选择用户名,其中country=:country and state=:state and sub=:sub');
$stmt->bindParam(':country',$\u GET['country']);
$stmt->bindParam(':state',$\u GET['state']);
$stmt->bindParam(':sub',$\u GET['sub'])
$stmt->execute();
而($rows=$stmt->fetch()){
echo$rows['username'];
回声“
”; }
这只有在三者都被约束的情况下才能起作用。如果没有收到任何结果,则不会返回任何结果

即使三者都未绑定,是否有可能使其工作

示例

http://www.example.com/index.php?country=US
将显示以下各项的结果:

从arraytest中选择用户名,其中country=US

http://www.example.com/index.php?country=US&state=california
将显示以下各项的结果:

从arraytest中选择用户名,其中country=US,state=california

http://www.example.com/index.php?country=US&state=california&sub=sanjose
将显示以下各项的结果:


从arraytest中选择用户名,其中country=US,state=california,sub=sanjose
准备
sql
,条件为

$sql = 'SELECT `username` FROM `arraytest` WHERE 1';

if(!empty($_GET['country'])) {
  $sql .= ' AND country = :country ';
}
if(!empty($_GET['state'])) {
  $sql .= ' AND state = :state ';
}
if(!empty($_GET['sub'])) {
  $sql .= ' AND sub = :sub ';
}

$stmt = $conn->prepare($sql);
bindParam
如下所示:

if(!empty($_GET['country'])) {
  $stmt->bindParam(':country', $_GET['country']);
}
if(!empty($_GET['state'])) {
  $stmt->bindParam(':state', $_GET['state']);
}
if(!empty($_GET['sub'])) {
  $stmt->bindParam(':sub', $_GET['sub']);
}

$stmt->execute();

准备带有条件的
sql

$sql = 'SELECT `username` FROM `arraytest` WHERE 1';

if(!empty($_GET['country'])) {
  $sql .= ' AND country = :country ';
}
if(!empty($_GET['state'])) {
  $sql .= ' AND state = :state ';
}
if(!empty($_GET['sub'])) {
  $sql .= ' AND sub = :sub ';
}

$stmt = $conn->prepare($sql);
bindParam
如下所示:

if(!empty($_GET['country'])) {
  $stmt->bindParam(':country', $_GET['country']);
}
if(!empty($_GET['state'])) {
  $stmt->bindParam(':state', $_GET['state']);
}
if(!empty($_GET['sub'])) {
  $stmt->bindParam(':sub', $_GET['sub']);
}

$stmt->execute();

像这样的事情应该可以做到

$sql = 'select username from arraytest where country = :country';

if(isset($_GET['state')) {
  $sql .= ' and state = :state';

if(isset($_GET['sub')) {
  $sql .= ' and sub = :sub';

$stmt = $conn->prepare($sql);

$stmt->bindParam(':country', isset($_GET['country']) ? $_GET['country'] : 'US');

if(isset($_GET['state')) {
  $stmt->bindParam(':state', $_GET['state']);

if(isset($_GET['sub')) {
  $stmt->bindParam(':sub', $_GET['sub']);
编辑:如果它在很多地方使用,你可能想做一个简单的函数,比如

那么你可以称之为

$stmt = buildstmt($conn, 'select username from arraytest',
                  array('country'=>'US', 'state'=>null, 'sub'=>null), $_GET);
$stmt->execute();

像这样的事情应该可以做到

$sql = 'select username from arraytest where country = :country';

if(isset($_GET['state')) {
  $sql .= ' and state = :state';

if(isset($_GET['sub')) {
  $sql .= ' and sub = :sub';

$stmt = $conn->prepare($sql);

$stmt->bindParam(':country', isset($_GET['country']) ? $_GET['country'] : 'US');

if(isset($_GET['state')) {
  $stmt->bindParam(':state', $_GET['state']);

if(isset($_GET['sub')) {
  $stmt->bindParam(':sub', $_GET['sub']);
编辑:如果它在很多地方使用,你可能想做一个简单的函数,比如

那么你可以称之为

$stmt = buildstmt($conn, 'select username from arraytest',
                  array('country'=>'US', 'state'=>null, 'sub'=>null), $_GET);
$stmt->execute();
我想到了这个:

$sql = 'select userName from arraytest where ';

if(!empty($_GET['country'])){ 
    echo 'Country Set <br>';
    $sql .= 'country = :country';
    $exe[':country'] = $_GET['country'];
} else { echo 'Country not set';}

if(!empty($_GET['state'])){ 
    echo 'State Set <br>';
    $sql .= ' and state = :state';
    $exe[':state'] = $_GET['state'];
} else { echo 'State not set';}

if(!empty($_GET['sub'])){ 
    echo 'Sub Set <br>';
    $sql .= ' and sub = :sub';
    $exe[':sub'] = $_GET['sub'];
}else{ echo 'Sub not set';}

print_r($exe);
$stmt = $conn->prepare($sql);

$stmt->execute($exe);

while($rows = $stmt->fetch()) {
    echo $rows['userName'];
    echo '<br>';
}
$sql='从arraytest中选择用户名,其中';
如果(!empty($_GET['country']){
echo“国家/地区集
”; $sql.='country=:country'; $exe[':country']=$\u GET['country']; }否则{echo'未设置国家/地区';} 如果(!empty($_GET['state']){ 回显“状态集
”; $sql.='和state=:state'; $exe[':state']=$\u GET['state']; }else{echo'状态未设置';} 如果(!empty($_GET['sub']){ 回显“子集
”; $sql.='和sub=:sub'; $exe[':sub']=$\u GET['sub']; }else{echo'子集合';} 打印(exe); $stmt=$conn->prepare($sql); $stmt->execute($exe); 而($rows=$stmt->fetch()){ echo$rows['userName']; 回声“
”; }
我想到了这个:

$sql = 'select userName from arraytest where ';

if(!empty($_GET['country'])){ 
    echo 'Country Set <br>';
    $sql .= 'country = :country';
    $exe[':country'] = $_GET['country'];
} else { echo 'Country not set';}

if(!empty($_GET['state'])){ 
    echo 'State Set <br>';
    $sql .= ' and state = :state';
    $exe[':state'] = $_GET['state'];
} else { echo 'State not set';}

if(!empty($_GET['sub'])){ 
    echo 'Sub Set <br>';
    $sql .= ' and sub = :sub';
    $exe[':sub'] = $_GET['sub'];
}else{ echo 'Sub not set';}

print_r($exe);
$stmt = $conn->prepare($sql);

$stmt->execute($exe);

while($rows = $stmt->fetch()) {
    echo $rows['userName'];
    echo '<br>';
}
$sql='从arraytest中选择用户名,其中';
如果(!empty($_GET['country']){
echo“国家/地区集
”; $sql.='country=:country'; $exe[':country']=$\u GET['country']; }否则{echo'未设置国家/地区';} 如果(!empty($_GET['state']){ 回显“状态集
”; $sql.='和state=:state'; $exe[':state']=$\u GET['state']; }else{echo'状态未设置';} 如果(!empty($_GET['sub']){ 回显“子集
”; $sql.='和sub=:sub'; $exe[':sub']=$\u GET['sub']; }else{echo'子集合';} 打印(exe); $stmt=$conn->prepare($sql); $stmt->execute($exe); 而($rows=$stmt->fetch()){ echo$rows['userName']; 回声“
”; }
…如果没有设置?所有数据?@JoachimIsaksson默认值为
US
…如果没有设置?所有数据?@JoachimIsaksson默认值为
US
@Norman这意味着查询是动态构造的,放入其中1会导致动态条件子句的更简单逻辑。@Norman这意味着查询是动态构造的,放入其中1会导致动态条件子句的更简单逻辑。虽然此解决方案有效,它不是很灵活。他可能想构造一个方法或函数,能够动态地完成这项工作。@MadaraUchiha添加了一个函数选项。虽然这个解决方案可行,但它不是很灵活。他可能想构造一个方法或函数,能够动态地完成这项工作。@MadaraUchiha添加了一个函数选项。