PHP脚本与数据库交互的意外结果

时间:2017-11-20 10:08:39

标签: php mysql database

我准备了一种方法" find"在PHP脚本中执行我的数据库中的一些搜索。它总是给我一个意想不到的结果,因为我搜索的东西确实存在于我的数据库中,但它显示我" bool(false)"。我没有&#39知道我犯错的地方。这是我的方法代码" find"和" findfirst"(对于findfirst,它与查找方法的概念相同。)

protected function _read($table,$params=[])
{
   $conditionString= '';
   $bind=[];
   $order='';
   $limit='';
//conditions
if(isset ($params['conditions']))
{
  if(is_array($params['conditions']))
  {
    foreach($params['conditions'] as $condition)
    {
      $conditionString.=' ' . $condition . ' AND';
    }
    $conditionString=trim($conditionString);
    $conditionString=rtrim($conditionString,' AND');

  }

  else {
  $conditionString= $params['conditions'];

  }

if($conditionString !=''){
 $conditionString=' Where' . $conditionString;
}

}
//bind
if (array_key_exists('bind',$params))
{
  $bind=$params['bind'];
}
//order
if (array_key_exists('order',$params))
{
  $order=' ORDER BY ' . $params['order'];
}


//limit
   if (array_key_exists('limit',$params))
{
  $limit=' LIMIT ' . $params['limit'];
}

$sql="SELECT * FROM {$table}{$conditionString}{$order}{$limit}";
if ($this->query($sql,$bind))
{
  if(!count($this->_result)) return false;
  return true;
}
return false;


}



public function find ($table ,$params=[])
{
if ($this->_read($table,$params)){
  return $this->results();
}
return false;
}

public function findfirst ($table ,$params=[])
{
  if ($this->_read($table,$params)){
  return $this->first();
}
return false;

}

这是home.php的代码,我调用方法"找到"

$usersU=$db->find('users',[

'conditions'=>"username = ?",
'bind'=>['kh'],
'order'=>"username, password",
'limit'=>''
]);

这个方法"结果"和"首先"。

public function results(){

  return $this->_result;
}

 public function first(){

  return (!empty($this->_result)) ? $this->_result[0] : [];
}

这是查询方法。



public function query ($sql, $params = [])
{
$this->_error = false;
if ($this->_query = $this->_pdo->prepare($sql))
{
  $x=1 ;
  if (count($params))
{
foreach ($params as $param)
{
  $this->_query->bindValue($x,$param);
  $x++;
}

}

if($this->_query->execute())
{

  $this->_result = $this->_query->fetchALL(PDO::FETCH_OBJ);
  $this->_count = $this->_query->rowCount();
  $this->_lastInsertID =$this->_pdo->lastInsertId();

}
else
{
   $this->_error= true;
}

}
return $this;

}




2 个答案:

答案 0 :(得分:0)

以下关键字PASSWORD (MySQL对保留关键字不区分大小写)是MySQL Reserved Keyword,因此必须正确引用以允许作为标识符。

示例

$usersU=$db->find('`users`',[    
'conditions'=>'`username` = ?',
'bind'=>['kh'],
'order'=>'`username`, `password`',
'limit'=>''
]);

尝试以下方法。该错误是因为$params['limit']$params['order']在创建查询的if语句中不应为空。

protected function _read($table,$params=[])
{
   $conditionString= '';
   $bind=[];
   $order='';
   $limit='';
//conditions
if(isset ($params['conditions']))
{
  if(is_array($params['conditions']))
  {
    foreach($params['conditions'] as $condition)
    {
      $conditionString.=' ' . $condition . ' AND';
    }
    $conditionString=trim($conditionString);
    $conditionString=rtrim($conditionString,' AND');

  }

  else {
  $conditionString= $params['conditions'];

  }

if($conditionString !=''){
 $conditionString=' Where ' . $conditionString;
}
}
//bind
if (array_key_exists('bind',$params))
{
  $bind=$params['bind'];
}
//order check that $params['order'] is not empty also
if (array_key_exists('order',$params) && !empty($params['order']))
{
  $order=' ORDER BY ' . $params['order'];
}

//limit check that $params['limit'] is not empty also
   if (array_key_exists('limit',$params) && !empty($params['limit']))
{
  $limit=' LIMIT ' . $params['limit'];
}

$sql="SELECT * FROM {$table}{$conditionString}{$order}{$limit}";
if ($this->query($sql,$bind))
{
  if(!count($this->_result)) return false;
  return true;
}
return false;      
}



public function find ($table ,$params=[])
{
if ($this->_read($table,$params)){
  return $this->results();
}
return false;
}

public function findfirst ($table ,$params=[])
{
  if ($this->_read($table,$params)){
  return $this->first();
}
return false;

}

答案 1 :(得分:0)

通过添加一些空格来解决{$ table} {$ conditionString} {$ order} {$ limit}