zoukankan      html  css  js  c++  java
  • mysql数据库 thinkphp连贯操作where条件的判断不正确的问题

    前两天一直写一个基于thinkphp的东西,遇到从mysql数据库里select数据,where条件一直出现问题的情况。直接上代码:

    $history = M('history');

    $suerId = $_SESSION['user_id'];

    $rs=$histroy->where('history_user_id = $userId')->select();

    上面代码$rs一直返回false。不得其解。

    后来gettype($userId) 返回string

    想是不是histroy_user_id匹配的是数字。然后把$userId改为数字4,可以取出数据。

    然后就把$userId改为(int)$userId,发现不管用,还是取不出数据。

    后来就看了看thinkphp的手册,手册上也没有做解释,但看到可以用数组的形式来写条件。随后就改为下面的形式可以select出数据。

    $history = M('history');

    $where['user_id'] = $_SESSION['user_id'];

    $histroy->where($where)->select();

    至于到底应该用上面类型的数据跟字段history_user_id来匹配至今不知。还请大神们拍砖,指导?

    根据大神们的指导:发现一下方法都是可行的。

    • 数组存放条件,如上面。$where['history_user_id']=$userId; $history->where($where)->select();此处要注意where数组的索引要与判断的数据库表的字段保持一致。
    • $history->where("history_user_id = $userId")->select(),where条件有变量的时候要用双引号,单引号里的变量不解析。
    • $history->where('history_user_id = '.$userId)->select() 也是可以的

    更新于2013.11.04

  • 相关阅读:
    Object类学习
    Thread.State 线程状态
    Thread.UncaughtExceptionHandler
    apply和call的区别
    如何实现border-width:0.5px;
    table固定头部,表格tbody可上下左右滑动
    canvas画布实现手写签名效果
    ES6学习笔记
    for循环中执行setTimeout问题
    javaScript函数提升及作用域
  • 原文地址:https://www.cnblogs.com/js-html/p/3398419.html
Copyright © 2011-2022 走看看