Php 绑定参数PDO准备语句
我真的不知道为什么在绑定参数之后会出现这个错误Php 绑定参数PDO准备语句,php,pdo,Php,Pdo,我真的不知道为什么在绑定参数之后会出现这个错误 SQLSTATE[HY093]: Invalid parameter number: no parameters were bound 这是我的功能 public function get_no_records($param){ $query = "SELECT * FROM users_tbl WHERE username = :value"; $this->query = $query; $stmt =
SQLSTATE[HY093]: Invalid parameter number: no parameters were bound
这是我的功能
public function get_no_records($param){
$query = "SELECT * FROM users_tbl WHERE username = :value";
$this->query = $query;
$stmt = $this->db->prepare($query);
$stmt->bindParam(':value',$param);
$stmt->execute();
$row_num = $stmt->rowCount();
if($row_num > 0){
$this->total_rec = $row_num;
return $row_num;
}
}
该函数是分页类的一部分。这是
<?php
class Paginator{
private $db;
public $page_no;//current page
public $limit;//record_per page
public $row_start;
public $total_rec;
public $query;
function __construct($con){
$this->db = $con;
}
//get total no of records
public function get_no_records($param){
$query = "SELECT * FROM users_tbl WHERE username = :value";
$this->query = $query;
$stmt = $this->db->prepare($query);
$stmt->bindParam(':value',$param);
$stmt->execute();
$row_num = $stmt->rowCount();
if($row_num > 0){
$this->total_rec = $row_num;
return $row_num;
}
}
public function get_data($limit,$page_no){
try {
$this->limit = $limit;
$this->page_no = $page_no;
if($this->limit == "all"){
$query = $this->query;
}
else{
$this->row_start = (($this->page_no-1) * $this->limit);
$query = $this->query . " LIMIT ". $this->row_start . "," . $this->limit;
}
$stmt = $this->db->prepare($query);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
//create an array to hold record
$results[] = $row;
}
$result = new stdClass();
$result->page_no = $this->page_no;
$result->limit = $this->limit;
$result->total_rec = $this->total_rec;
$result->data = $results;
return $result;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function create_links($links,$list_class){
if($this->limit == 'all'){
return '';
}
$last = ceil($this->total_rec/$this->limit);
$start = (($this->page_no - $links) > 0) ? $this->page_no - $links : 1;
$end = (($this->page_no + $links) < $last) ? $this->page_no + $links : $last;
$html = '<ul class="' . $list_class . '">';
$class = ($this->page_no == 1) ? "disabled" : "";
$previous_page = ($this->page_no == 1) ?
'<a href= ""><li class="' . $class . '">«</a></li>' :
'<li class="' . $class . '"><a href="?limit=' . $this->limit . '&page_no=' . ($this->page_no-1) . '">«</a></li>';
$html .= $previous_page;
if($start > 1){
$html .= '<li><a href="?limit=' . $this->limit . '&page_no=1">1</a></li>';
$html .= '<li class="disabled"><span>....</span></li>';
}
for($i = $start;$i<=$end;$i++){
$class = ($this->page_no == $i)? "active" : "";
$html .= '<li class="' . $class . '"><a href="?limit=' . $this->limit . '&page_no=' . $i .'">' . $i . '</a></li>';
}
if( $end < $last){
$html .= '<li class="disabled"><span>....</span></li>';
$html .= '<li><a href="?limit=' . $this->limit . '&page_no=' . $last . '">' . $last . '</a></li>';
}
$class = ($this->page_no == $last)? "disabled" : "";
$next_page = ( $this->page_no == $last)?
'<li class="' . $class . '"><a href="">»</a></li>':
'<li class="' . $class . '"><a href="?limit=' . $this->limit . '&page_no=' . ($this->page_no + 1) . '">»</a></li>';
$html .= $next_page;
$html .= '</ul>';
return $html;
}
我不使用bind param,只使用execute语句
public function get_no_records($param)
{
$query = "SELECT * FROM users_tbl WHERE username = :my_value;";
$this->query = $query; //What use do I have?
$stmt = $this->db->prepare($query);
$stmt->execute(array(':my_value' => $param));
$row_num = $stmt->rowCount();
if($row_num > 0)
{
$this->total_rec = $row_num;
return $row_num;
}
return 0; //Added so the function always has a return value
}
':value'
的绑定是通过创建一个关联数组来完成的,该数组仅用作execute函数的参数。感谢大家。我已经得到了错误的来源,它是get_data(),因为该函数还包含一个查询,所以还必须绑定一个值
<?php
class Paginator{
private $db;
public $page_no;//current page
public $limit;//record_per page
public $row_start;
public $total_rec;
public $query;
function __construct($con){
$this->db = $con;
}
//get total no of records
public function get_no_records($query,$value){
$this->query = $query;
$stmt = $this->db->prepare($query);
$stmt->bindParam(':value',$value);
$stmt->execute();
$row_num = $stmt->rowCount();
if($row_num > 0){
$this->total_rec = $row_num;
return $row_num;
}
}
public function get_data($limit,$page_no,$value){
try {
$this->limit = $limit;
$this->page_no = $page_no;
if($this->limit == "all"){
$query = $this->query;
}
else{
$this->row_start = (($this->page_no-1) * $this->limit);
$query = $this->query . " LIMIT ". $this->row_start . "," . $this->limit;
}
$stmt = $this->db->prepare($query);
$stmt->bindParam('value',$value);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
//create an array to hold record
$results[] = $row;
}
if(empty($results)){
return;
}
$result = new stdClass();
$result->page_no = $this->page_no;
$result->limit = $this->limit;
$result->total_rec = $this->total_rec;
$result->data = $results;
return $result;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function create_links($links,$list_class){
if($this->limit == 'all'){
return '';
}
$last = ceil($this->total_rec/$this->limit);
$start = (($this->page_no - $links) > 0) ? $this->page_no - $links : 1;
$end = (($this->page_no + $links) < $last) ? $this->page_no + $links : $last;
$html = '<ul class="' . $list_class . '">';
$class = ($this->page_no == 1) ? "disabled" : "";
$previous_page = ($this->page_no == 1) ?
'<a href= ""><li class="' . $class . '">«</a></li>' :
'<li class="' . $class . '"><a href="?limit=' . $this->limit . '&page_no=' . ($this->page_no-1) . '">«</a></li>';
$html .= $previous_page;
if($start > 1){
$html .= '<li><a href="?limit=' . $this->limit . '&page_no=1">1</a></li>';
$html .= '<li class="disabled"><span>....</span></li>';
}
for($i = $start;$i<=$end;$i++){
$class = ($this->page_no == $i)? "active" : "";
$html .= '<li class="' . $class . '"><a href="?limit=' . $this->limit . '&page_no=' . $i .'">' . $i . '</a></li>';
}
if( $end < $last){
$html .= '<li class="disabled"><span>....</span></li>';
$html .= '<li><a href="?limit=' . $this->limit . '&page_no=' . $last . '">' . $last . '</a></li>';
}
$class = ($this->page_no == $last)? "disabled" : "";
$next_page = ( $this->page_no == $last)?
'<li class="' . $class . '"><a href="">»</a></li>':
'<li class="' . $class . '"><a href="?limit=' . $this->limit . '&page_no=' . ($this->page_no + 1) . '">»</a></li>';
$html .= $next_page;
$html .= '</ul>';
return $html;
}
}
?>
在执行查询之前,get_data()中的查询现在有一个$value绑定的参数。什么类型是$param
?也许可以考虑说明它的类型是字符串.. @ JRSZ AV尝试…我的问题已经被更新了类的函数包含in@RyanVincent我不明白你的意思。摆脱所有的“尝试捕捉”操作符。2.编辑您的问题并添加您将收到的完整错误消息。3.突出显示错误消息指向的代码行。谢谢。但是我仍然有相同的结果,get_no_records($param)在一个类中,我在类中有一个公共变量,在这个类中我分配了查询,你得到的是完全相同的错误吗?数据库中是否存在所有的表和列?我得到了完全相同的错误,所有的表和列都存在,所以让我们一步一步地检查所有内容。你能在你遇到问题的函数中执行简单的查询吗?像SELECT*FROM users\u tbl代码>?好的,谢谢。但是在这种情况下,我必须注释掉binparam,并且不向方法传递任何参数。很高兴您调试并修复了它。:)