PHP PDO动态查询构建
执行此查询时,会显示一条警告,如 警告:PDOStatement::execute():SQLSTATE[HY093]:无效参数编号:未定义参数 有什么问题吗?而不是PHP PDO动态查询构建,php,mysql,pdo,Php,Mysql,Pdo,执行此查询时,会显示一条警告,如 警告:PDOStatement::execute():SQLSTATE[HY093]:无效参数编号:未定义参数 有什么问题吗?而不是 $arr[]=“:from_date=>$from_date” 做 $arr['from_date']=$from_date 阅读有关php数组的更多信息 另外,我要做的是: $arr = array(); $from_date = '2015-01-01'; $to_date = '2015-01-31'; $order_no
$arr[]=“:from_date=>$from_date”代码>
做
$arr['from_date']=$from_date代码>
阅读有关php数组的更多信息
另外,我要做的是:
$arr = array();
$from_date = '2015-01-01';
$to_date = '2015-01-31';
$order_no = '25215';
$sql = "SELECT * FROM test";
if(!empty($from_date)&&!empty($to_date))
{
$sql.=" WHERE txn_date BETWEEN :from_date AND :to_date";
$arr[] = ":from_date => $from_date";
$arr[] = ":to_date => $to_date";
$condition=true;
}
if(!empty($order_no))
{
if($condition)
{
$sql.=" AND ref_number = :order_no";
$arr[] = ":order_no => $order_no";
}
else
{
$sql.=" WHERE ref_number = :order_no";
$arr[] = ":order_no => $order_no";
$condition=true;
}
}
$stmt = $db->prepare($sql);
$stmt->execute($arr);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
这样就不需要测试$condition
,只需连接到$sql
而不是
$arr[]=“:from_date=>$from_date”代码>
做
$arr['from_date']=$from_date代码>
阅读有关php数组的更多信息
另外,我要做的是:
$arr = array();
$from_date = '2015-01-01';
$to_date = '2015-01-31';
$order_no = '25215';
$sql = "SELECT * FROM test";
if(!empty($from_date)&&!empty($to_date))
{
$sql.=" WHERE txn_date BETWEEN :from_date AND :to_date";
$arr[] = ":from_date => $from_date";
$arr[] = ":to_date => $to_date";
$condition=true;
}
if(!empty($order_no))
{
if($condition)
{
$sql.=" AND ref_number = :order_no";
$arr[] = ":order_no => $order_no";
}
else
{
$sql.=" WHERE ref_number = :order_no";
$arr[] = ":order_no => $order_no";
$condition=true;
}
}
$stmt = $db->prepare($sql);
$stmt->execute($arr);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
这样就不需要测试$condition
,只需连接到$sql
而不是
$arr[]=“:from_date=>$from_date”代码>
做
$arr['from_date']=$from_date代码>
阅读有关php数组的更多信息
另外,我要做的是:
$arr = array();
$from_date = '2015-01-01';
$to_date = '2015-01-31';
$order_no = '25215';
$sql = "SELECT * FROM test";
if(!empty($from_date)&&!empty($to_date))
{
$sql.=" WHERE txn_date BETWEEN :from_date AND :to_date";
$arr[] = ":from_date => $from_date";
$arr[] = ":to_date => $to_date";
$condition=true;
}
if(!empty($order_no))
{
if($condition)
{
$sql.=" AND ref_number = :order_no";
$arr[] = ":order_no => $order_no";
}
else
{
$sql.=" WHERE ref_number = :order_no";
$arr[] = ":order_no => $order_no";
$condition=true;
}
}
$stmt = $db->prepare($sql);
$stmt->execute($arr);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
这样就不需要测试$condition
,只需连接到$sql
而不是
$arr[]=“:from_date=>$from_date”代码>
做
$arr['from_date']=$from_date代码>
阅读有关php数组的更多信息
另外,我要做的是:
$arr = array();
$from_date = '2015-01-01';
$to_date = '2015-01-31';
$order_no = '25215';
$sql = "SELECT * FROM test";
if(!empty($from_date)&&!empty($to_date))
{
$sql.=" WHERE txn_date BETWEEN :from_date AND :to_date";
$arr[] = ":from_date => $from_date";
$arr[] = ":to_date => $to_date";
$condition=true;
}
if(!empty($order_no))
{
if($condition)
{
$sql.=" AND ref_number = :order_no";
$arr[] = ":order_no => $order_no";
}
else
{
$sql.=" WHERE ref_number = :order_no";
$arr[] = ":order_no => $order_no";
$condition=true;
}
}
$stmt = $db->prepare($sql);
$stmt->execute($arr);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
然后就不需要测试$condition
,只需将变量$arr中的$sql
连接起来,就可以像这样替换所有关联索引
$sql = "SELECT * FROM test WHERE 1=1";
另外,在外部块中移动公共代码,而不是在if/else梯形图中移动公共代码也是一种很好的做法
$arr[':from_date'] = $from_date;
$arr[':to_date'] = $to_date;
$arr[':order_no'] = $order_no;
正如op在评论中所问,这里是对此的解释
if(!empty($order_no))
{
$arr[':order_no'] = $order_no;
if($condition)
{
$sql.=" AND ref_number = :order_no";
}
else
{
$sql.=" WHERE ref_number LIKE :order_no";
$condition=true;
}
}
把这个换成这个
$sql.=" WHERE txn_date BETWEEN :from_date AND :to_date";
在if/else语句的where条件中,将其转换为无符号整数,而不是上述类型
$sql.=" WHERE date(txn_date) BETWEEN date(:from_date) AND date(:to_date)";
ref_number = :order_no
在变量$arr中,所有关联索引都应该这样替换
$sql = "SELECT * FROM test WHERE 1=1";
另外,在外部块中移动公共代码,而不是在if/else梯形图中移动公共代码也是一种很好的做法
$arr[':from_date'] = $from_date;
$arr[':to_date'] = $to_date;
$arr[':order_no'] = $order_no;
正如op在评论中所问,这里是对此的解释
if(!empty($order_no))
{
$arr[':order_no'] = $order_no;
if($condition)
{
$sql.=" AND ref_number = :order_no";
}
else
{
$sql.=" WHERE ref_number LIKE :order_no";
$condition=true;
}
}
把这个换成这个
$sql.=" WHERE txn_date BETWEEN :from_date AND :to_date";
在if/else语句的where条件中,将其转换为无符号整数,而不是上述类型
$sql.=" WHERE date(txn_date) BETWEEN date(:from_date) AND date(:to_date)";
ref_number = :order_no
在变量$arr中,所有关联索引都应该这样替换
$sql = "SELECT * FROM test WHERE 1=1";
另外,在外部块中移动公共代码,而不是在if/else梯形图中移动公共代码也是一种很好的做法
$arr[':from_date'] = $from_date;
$arr[':to_date'] = $to_date;
$arr[':order_no'] = $order_no;
正如op在评论中所问,这里是对此的解释
if(!empty($order_no))
{
$arr[':order_no'] = $order_no;
if($condition)
{
$sql.=" AND ref_number = :order_no";
}
else
{
$sql.=" WHERE ref_number LIKE :order_no";
$condition=true;
}
}
把这个换成这个
$sql.=" WHERE txn_date BETWEEN :from_date AND :to_date";
在if/else语句的where条件中,将其转换为无符号整数,而不是上述类型
$sql.=" WHERE date(txn_date) BETWEEN date(:from_date) AND date(:to_date)";
ref_number = :order_no
在变量$arr中,所有关联索引都应该这样替换
$sql = "SELECT * FROM test WHERE 1=1";
另外,在外部块中移动公共代码,而不是在if/else梯形图中移动公共代码也是一种很好的做法
$arr[':from_date'] = $from_date;
$arr[':to_date'] = $to_date;
$arr[':order_no'] = $order_no;
正如op在评论中所问,这里是对此的解释
if(!empty($order_no))
{
$arr[':order_no'] = $order_no;
if($condition)
{
$sql.=" AND ref_number = :order_no";
}
else
{
$sql.=" WHERE ref_number LIKE :order_no";
$condition=true;
}
}
把这个换成这个
$sql.=" WHERE txn_date BETWEEN :from_date AND :to_date";
在if/else语句的where条件中,将其转换为无符号整数,而不是上述类型
$sql.=" WHERE date(txn_date) BETWEEN date(:from_date) AND date(:to_date)";
ref_number = :order_no
您的ref\u编号,如:order\u no
可能是ref\u编号=:order\u no
鉴于您没有通配符,您的ref\u编号,如:order\u no
可能是ref\u编号=:order\u no
鉴于您没有通配符,您的ref\u编号,如:order\u no
可能是ref\u number=:order\u no
既然你没有通配符,你的ref\u number,比如:order\u no
也可以是ref\u number=:order\u no
既然你没有通配符,谢谢。日期和订单号是字符串字段。如何处理它?您的意思是在sql表中,日期和顺序的列是varchar数据类型,对吗?是的。因此,当我执行查询时,没有行返回。现在这个数组就像数组一样([从\u日期]=>2014-01-01[到\u日期]=>2014-01-31)。但我需要像数组一样([from_date]=>“2014-01-01”[to_date]=>“2014-01-31”)将所有列转换为各自的数据类型,或者在sql查询类型中转换,例如,请参见此处,以获取感谢。日期和订单号是字符串字段。如何处理它?您的意思是在sql表中,日期和顺序的列是varchar数据类型,对吗?是的。因此,当我执行查询时,没有行返回。现在这个数组就像数组一样([从\u日期]=>2014-01-01[到\u日期]=>2014-01-31)。但我需要像数组一样([from_date]=>“2014-01-01”[to_date]=>“2014-01-31”)将所有列转换为各自的数据类型,或者在sql查询类型中转换,例如,请参见此处,以获取感谢。日期和订单号是字符串字段。如何处理它?您的意思是在sql表中,日期和顺序的列是varchar数据类型,对吗?是的。因此,当我执行查询时,没有行返回。现在这个数组就像数组一样([从\u日期]=>2014-01-01[到\u日期]=>2014-01-31)。但我需要像数组一样([from_date]=>“2014-01-01”[to_date]=>“2014-01-31”)将所有列转换为各自的数据类型,或者在sql查询类型中转换,例如,请参见此处,以获取感谢。日期和订单号是字符串字段。如何处理它?您的意思是在sql表中,日期和顺序的列是varchar数据类型,对吗?是的。因此,当我执行查询时,没有行返回。现在这个数组就像数组一样([从\u日期]=>2014-01-01[到\u日期]=>2014-01-31)。但我需要像数组一样([from_date]=>“2014-01-01”[to_date]=>“2014-01-31”)将所有列转换为各自的数据类型,或者在sql查询类型中转换,例如,我理解。非常感谢。还有一件事,日期和订单号是字符串字段。怎么办?我明白。非常感谢。还有一件事,日期和订单号是字符串字段。怎么办?我明白。非常感谢。还有一件事,日期和订单号是字符串字段。怎么办?我明白。非常感谢。还有一件事,日期和订单号是字符串字段。你怎么处理呢?