sql语句字符拼接

sql语句字符串拼接中容易产生 前后语句没有加空格 导致在sql 中运行时全连在一起,无法识别的问题

错误代码:

$where  = 'where 1=1';//where前
    if($status!="all"){
      $where.="and p.status='{$status}'";//and 前 '{$status}'后没有空格
    };
    if($categoryId!="all"){
      $where.="and p.category_id ='{$categoryId}'";//and前 '{$categoryId}'后没有空格
    }
    $connect = connect();
    $sql = "SELECT p.id,p.title,p.created,p.`status`,u.nickname,c.`name` FROM posts p
    left JOIN users u ON u.id = p.user_id
    LEFT JOIN categories c ON c.id = p.category_id". $where ."limit {$offset},{$pageSize}";

运行结果:

运行结果

在上面所说位置加上空格即可

猜你喜欢

转载自blog.csdn.net/yu3jian4/article/details/83829882