dongtangze6393 2014-11-12 01:25
浏览 41

为什么这个PHP PDO函数不会从数据库中返回值?

I have a web application which accepts an Excel spreadsheet, parses the data, and adds that data to a MySQL database. Some of the sheets are fine, everything works as expected. However some sheets are not returning true when they should. Before data is entered I have a general purpose function which will check that table for the value and then return true or false. This function looks like this:

//Check if a sql will return with any values
function tableCheck($table, $column, $value){
  //PDO Connecttion
  $core = Core::getInstance();

  $sql = "SELECT * FROM $table WHERE $column = :value;";

  //Create a prepared statement
  $stmt = $core->dbh->prepare($sql);
  $stmt->bindParam(':value', $value, PDO::PARAM_STR);

  $stmt->execute();

  //return true if there is a hit on that value
  if($stmt->rowCount() > 0){
    return true;
  } else {
    return false;
  }

}

Like I said this works some of the time and some of the time it doesn't and I get tons of repeated values from that sheet. Obviously this seriously messes with my data.

At first I thought it had to do with special characters but I have since found sheets where this field does not have special characters have a similar problem. This problem only occurs on a single column as well. Other fields of the sheet parse perfectly fine in all cases.

Any idea on what could be causing this problem?

EDIT: I also want to note that if i copy/paste the data into MySQL workbench or the command line it does return the rows.

  • 写回答

2条回答 默认 最新

  • douji5746 2014-11-12 01:35
    关注

    This looks dangerous, but I'm sure you're doing some sort of filtering, right? :)

    Sounds to me like some of your table or column names could be reserved words, for example AS and BY.

    Put ticks around them in your query:

    $sql = "SELECT * FROM `$table` WHERE `$column` = :value;";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?