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

标签 php mysql database

我在 PHP 脚本中准备了一个方法“find”来在我的数据库中执行一些搜索。它总是给我一个意想不到的结果,因为我搜索的东西确实存在于我的数据库中,但它告诉我“bool(false)”。我不知道我在哪里犯了错误。这是我的方法“find”和“findfirst”的代码(对于 findfirst,它与 find 方法的概念相同)。

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;

}

最佳答案

以下关键字 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;

}

关于php - PHP 脚本与数据库交互的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47389482/

相关文章:

PHP jQuery json_encode

php - 我以前从未遇到过的 PHP 符号

php - 在 PHP 中的日期格式之间转换给出错误

mysql - 错误 (1142) 使用 mysqldump 在 session_variables 表上拒绝 SELECT 命令

mysql - 数据插入后如何为sql表创建索引号列?

mysql - 使用 COUNT() 查找另一个表中存在多少行

php - 如何访问另一个页面上另一个类中的mysqli连接?

php - xdebug 断点有效但跳过不

Python/Django 处理错误 UNIQUE 约束

database - 表正在发生变化。触发器可能看不到它(即使在不同的表上)